package com.boot.utils;

import com.boot.web.dao.*;
import com.boot.web.dto.AppMailListDTO;
import com.boot.web.entity.*;
import com.boot.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 业务定时处理
 * @author zhouJie
 */
@Component
@Configuration
@EnableScheduling
public class TimingUtil {

    @Autowired
    private PrizePoolDao prizePoolDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private OpenRedPackDao openRedPackDao;

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private AccountRecordDao accountRecordDao;

    @Autowired
    private OrdersCopyService ordersCopyService;

    @Autowired
    private OrdersCopyDao ordersCopyDao;

    @Autowired
    private CollageModeDao collageModeDao;

    @Autowired
    private SkuRelationDao skuRelationDao;

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private CollageInRecord3Dao collageInRecord3Dao;

    @Autowired
    private UserCouponDao userCouponDao;

    @Autowired
    private ModeProductDao modeProductDao;

    @Autowired
    private CollageInRecord2Dao collageInRecord2Dao;

    @Autowired
    private OrderDetailsDao orderDetailsDao;

    @Autowired
    private CollageUserDao collageUserDao;

    @Autowired
    private CollageInRecord3CopyDao collageInRecord3CopyDao;

    @Autowired
    private FailRedPackDao failRedPackDao;

    @Autowired
    private CollageInRecord20Dao collageInRecord20Dao;

    @Autowired
    private GoodsPrizePoolDao goodsPrizePoolDao;

    @Autowired
    private GoodsSplitAccountDao goodsSplitAccountDao;

    @Autowired
    private OrdersCopy30Dao ordersCopy30Dao;

    @Autowired
    private OrdersCopy30Service ordersCopy30Service;

    @Autowired
    private TodayPrizeDao todayPrizeDao;

    @Autowired
    private CollageInRecord30Dao collageInRecord30Dao;

    @Autowired
    private CollageInRecord30CopyDao collageInRecord30CopyDao;

    @Autowired
    private TodayCollageVoucherDao todayCollageVoucherDao;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private OrdersShareRepeatDao ordersShareRepeatDao;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrdersShareRepeatService ordersShareRepeatService;

    @Autowired
    private TodayCollageProfitDao todayCollageProfitDao;

    @Autowired
    private PrizePoolWeeklyDao prizePoolWeeklyDao;

    @Autowired
    private PrizePoolAreanDao prizePoolAreanDao;

    @Autowired
    private CollageTotalVoucherDao collageTotalVoucherDao;


    private static TimingUtil tc;

    // 所有引入的dao或service生成set方法

    public void setPrizePoolDao(PrizePoolDao prizePoolDao) {
        this.prizePoolDao = prizePoolDao;
    }

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void setOpenRedPackDao(OpenRedPackDao openRedPackDao) {
        this.openRedPackDao = openRedPackDao;
    }

    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    public void setAccountRecordDao(AccountRecordDao accountRecordDao) {
        this.accountRecordDao = accountRecordDao;
    }

    public void setOrdersCopyService(OrdersCopyService ordersCopyService) {
        this.ordersCopyService = ordersCopyService;
    }

    public void setOrdersCopyDao(OrdersCopyDao ordersCopyDao) {
        this.ordersCopyDao = ordersCopyDao;
    }

    public void setCollageModeDao(CollageModeDao collageModeDao) {
        this.collageModeDao = collageModeDao;
    }

    public void setSkuRelationDao(SkuRelationDao skuRelationDao) {
        this.skuRelationDao = skuRelationDao;
    }

    public void setOrdersDao(OrdersDao ordersDao) {
        this.ordersDao = ordersDao;
    }

    public void setCollageInRecord3Dao(CollageInRecord3Dao collageInRecord3Dao) {
        this.collageInRecord3Dao = collageInRecord3Dao;
    }

    public void setUserCouponDao(UserCouponDao userCouponDao) {
        this.userCouponDao = userCouponDao;
    }

    public void setModeProductDao(ModeProductDao modeProductDao) {
        this.modeProductDao = modeProductDao;
    }

    public void setCollageInRecord2Dao(CollageInRecord2Dao collageInRecord2Dao) {
        this.collageInRecord2Dao = collageInRecord2Dao;
    }

    public void setOrderDetailsDao(OrderDetailsDao orderDetailsDao) {
        this.orderDetailsDao = orderDetailsDao;
    }

    public void setCollageUserDao(CollageUserDao collageUserDao) {
        this.collageUserDao = collageUserDao;
    }

    public void setCollageInRecord3CopyDao(CollageInRecord3CopyDao collageInRecord3CopyDao) {
        this.collageInRecord3CopyDao = collageInRecord3CopyDao;
    }

    public void setFailRedPackDao(FailRedPackDao failRedPackDao) {
        this.failRedPackDao = failRedPackDao;
    }

    public void setCollageInRecord20Dao(CollageInRecord20Dao collageInRecord20Dao) {
        this.collageInRecord20Dao = collageInRecord20Dao;
    }

    public void setGoodsPrizePoolDao(GoodsPrizePoolDao goodsPrizePoolDao) {
        this.goodsPrizePoolDao = goodsPrizePoolDao;
    }

    public void setGoodsSplitAccountDao(GoodsSplitAccountDao goodsSplitAccountDao) {
        this.goodsSplitAccountDao = goodsSplitAccountDao;
    }

    public void setOrdersCopy30Dao(OrdersCopy30Dao ordersCopy30Dao) {
        this.ordersCopy30Dao = ordersCopy30Dao;
    }

    public void setOrdersCopy30Service(OrdersCopy30Service ordersCopy30Service) {
        this.ordersCopy30Service = ordersCopy30Service;
    }

    public void setTodayPrizeDao(TodayPrizeDao todayPrizeDao) {
        this.todayPrizeDao = todayPrizeDao;
    }

    public void setCollageInRecord30Dao(CollageInRecord30Dao collageInRecord30Dao) {
        this.collageInRecord30Dao = collageInRecord30Dao;
    }

    public void setCollageInRecord30CopyDao(CollageInRecord30CopyDao collageInRecord30CopyDao) {
        this.collageInRecord30CopyDao = collageInRecord30CopyDao;
    }

    public void setTodayCollageVoucherDao(TodayCollageVoucherDao todayCollageVoucherDao) {
        this.todayCollageVoucherDao = todayCollageVoucherDao;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public void setOrdersShareRepeatDao(OrdersShareRepeatDao ordersShareRepeatDao) {
        this.ordersShareRepeatDao = ordersShareRepeatDao;
    }

    public void setOrdersService(OrdersService ordersService) {
        this.ordersService = ordersService;
    }

    public void setOrdersShareRepeatService(OrdersShareRepeatService ordersShareRepeatService) {
        this.ordersShareRepeatService = ordersShareRepeatService;
    }

    public void setTodayCollageProfitDao(TodayCollageProfitDao todayCollageProfitDao) {
        this.todayCollageProfitDao = todayCollageProfitDao;
    }

    public void setPrizePoolWeeklyDao(PrizePoolWeeklyDao prizePoolWeeklyDao) {
        this.prizePoolWeeklyDao = prizePoolWeeklyDao;
    }

    public void setPrizePoolAreanDao(PrizePoolAreanDao prizePoolAreanDao) {
        this.prizePoolAreanDao = prizePoolAreanDao;
    }

    public void setCollageTotalVoucherDao(CollageTotalVoucherDao collageTotalVoucherDao) {
        this.collageTotalVoucherDao = collageTotalVoucherDao;
    }

    @PostConstruct
    public void init() {
        tc = this;
        tc.prizePoolDao = this.prizePoolDao;
        tc.productDao = this.productDao;
        tc.accountDao = this.accountDao;
        tc.openRedPackDao = this.openRedPackDao;
        tc.customerDao = this.customerDao;
        tc.accountRecordDao = this.accountRecordDao;
        tc.ordersCopyService = this.ordersCopyService;
        tc.ordersCopyDao = this.ordersCopyDao;
        tc.collageModeDao = this.collageModeDao;
        tc.skuRelationDao = this.skuRelationDao;
        tc.collageInRecord3Dao = this.collageInRecord3Dao;
        tc.userCouponDao = this.userCouponDao;
        tc.modeProductDao = this.modeProductDao;
        tc.collageInRecord2Dao = this.collageInRecord2Dao;
        tc.orderDetailsDao = this.orderDetailsDao;
        tc.collageUserDao = this.collageUserDao;
        tc.collageInRecord3CopyDao = this.collageInRecord3CopyDao;
        tc.failRedPackDao = this.failRedPackDao;
        tc.collageInRecord20Dao = this.collageInRecord20Dao;
        tc.goodsPrizePoolDao = this.goodsPrizePoolDao;
        tc.goodsSplitAccountDao = this.goodsSplitAccountDao;
        tc.ordersCopy30Dao = this.ordersCopy30Dao;
        tc.ordersCopy30Service = this.ordersCopy30Service;
        tc.todayPrizeDao = this.todayPrizeDao;
        tc.collageInRecord30Dao = this.collageInRecord30Dao;
        tc.collageInRecord30CopyDao = this.collageInRecord30CopyDao;
        tc.todayCollageVoucherDao = this.todayCollageVoucherDao;
        tc.customerService = this.customerService;
        tc.ordersShareRepeatDao = this.ordersShareRepeatDao;
        tc.ordersService = this.ordersService;
        tc.ordersShareRepeatService = this.ordersShareRepeatService;
        tc.todayCollageProfitDao = this.todayCollageProfitDao;
        tc.prizePoolWeeklyDao = this.prizePoolWeeklyDao;
        tc.prizePoolAreanDao = this.prizePoolAreanDao;
        tc.collageTotalVoucherDao = this.collageTotalVoucherDao;
    }


    /**
     *      * 在给定的日期加上或减去指定月份后的日期
     *      *
     *      * @param sourceDate 原始时间
     *      * @param month      要调整的月份，向前为负数，向后为正数
     *      * @return
     *
     */
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);
        return c.getTime();
    }


    // 每天晚上02:00将礼包下架
    //@Scheduled(cron = "0 00 2 * * ?")
    public void TimingBusiness1() {
        System.err.println("02:00 定时任务开始 -------------------------------------------------------------");
        List<Product> productList = tc.productDao.queryProductByType3();
        if (productList.size() > 0) {
            for (Product product : productList) {
                product.setIsShelf("0");
                tc.productDao.update(product);
            }
            System.err.println("02:00 内容：礼包已下架 -------------------------------------------------------------");
        }
        System.err.println("02:00 定时任务结束 -------------------------------------------------------------");
    }


    // 统计今日奖金池（要分的钱和剩余的钱）和统计红包券，给有红包券的人生成红包数据
    //@Scheduled(cron = "0 05 2 * * ?")
    public void TimingBusiness2(){
        System.err.println("02:05 定时任务开始 -------------------------------------------------------------");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        System.err.println("02:15 内容：当前时间 -------------------------------------------------------------" + sdf.format(now.getTime()));
        now.add(Calendar.DAY_OF_MONTH, -1);
        String date1 = sdf.format(now.getTime());
        System.err.println("02:15 内容：当前时间的前一天 -------------------------------------------------------------" + date1);
        PrizePool prizePool = tc.prizePoolDao.queryByToday(date1);
        if(prizePool != null){
            // TODO 总金额 prizePool.getTotalMoney()
            // 今日要分的钱
            prizePool.setOneThird(prizePool.getTotalMoney().divide(new BigDecimal(3), 2, BigDecimal.ROUND_HALF_UP));
            // 剩余的钱
            prizePool.setSurplusMoney(prizePool.getTotalMoney().subtract(prizePool.getOneThird()));
            tc.prizePoolDao.update(prizePool);
            System.err.println("02:05 内容：今日奖金池金额已调整（总的/要分的/剩余的） -------------------------------------------------------------");
            List<Account> accountList = tc.accountDao.queryAccountByRedPack();
            if(accountList.size() > 0){
                int index = 1;
                for(Account account : accountList){
                    Integer quantity = account.getRedPacket();
                    for(int i = 0; i < quantity; i++){
                        OpenRedPack redPack = new OpenRedPack();
                        redPack.setIsShow("2");
                        redPack.setStatus("2");
                        redPack.setUserId(account.getUserId());
                        redPack.setRedPackQuantity(1);
                        redPack.setPrizeMoney(new BigDecimal(0));
                        redPack.setUpdateTime(new Date());
                        redPack.setSerialNumber(index++);
                        tc.openRedPackDao.save(redPack);
                    }
                }
                System.err.println("02:05 内容：当日红包券已生成红包分配到个人 -------------------------------------------------------------");
            }
        }
        System.err.println("02:05 定时任务结束 -------------------------------------------------------------");
    }


    // 分配各个红包金额
    //@Scheduled(cron = "0 15 2 * * ?")
    public void TimingBusiness3(){
        System.err.println("02:15 定时任务开始 -------------------------------------------------------------");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        System.err.println("02:15 内容：当前时间 -------------------------------------------------------------" + sdf.format(now.getTime()));
        now.add(Calendar.DAY_OF_MONTH, -1);
        String date1 = sdf.format(now.getTime());
        System.err.println("02:15 内容：当前时间的前一天 -------------------------------------------------------------" + date1);
        PrizePool prizePool = tc.prizePoolDao.queryByToday(date1);
        String time = sdf.format(new Date());
        List<OpenRedPack> openRedPackList = tc.openRedPackDao.queryByToday(time);
        int index = openRedPackList.size();
        if(index > 0){ // 75
            BigDecimal branch = null;
            BigDecimal bigDecimal369 = new BigDecimal(369);
            if(index == 10){
                if(prizePool.getOneThird().doubleValue() >= 369){
                    OpenRedPack openRedPack;
                    int d;
                    do {
                        d = 1 + (int)(Math.random()*(index + 1 - 1));
                        openRedPack = tc.openRedPackDao.queryByCondition(d);
                        if(openRedPack.getPrizeMoney().doubleValue() == 0){
                            break;
                        }
                    } while (openRedPack.getPrizeMoney().doubleValue() > 0);
                    if(openRedPack != null){
                        openRedPack.setPrizeMoney(bigDecimal369);
                        openRedPack.setUpdateTime(new Date());
                        tc.openRedPackDao.update(openRedPack);
                        branch = prizePool.getOneThird().subtract(bigDecimal369);
                        if(branch.doubleValue() >= 0){
                            int x = prizePool.getRedPackQuantity() - 1; // 9
                            List<Double> al = RandomNumberUtil.splitRedPacket(branch.doubleValue(), x, 60, 180);
                            if(al.size() > 0){
                                System.err.println("开始分普通红包 = 10 -------------------------------------------------------------");
                                List<OpenRedPack> openRedPackList1 = tc.openRedPackDao.queryByToday1(time);
                                for(int y = 0; y < openRedPackList1.size(); y++){
                                    OpenRedPack open = openRedPackList1.get(y);
                                    open.setPrizeMoney(new BigDecimal(al.get(y)));
                                    open.setUpdateTime(new Date());
                                    tc.openRedPackDao.update(open);
                                }
                            }
                        }
                    }
                }
            }else if(index > 10){
                int dou = (int)Math.round(index * 0.1); // 10%
                for(int j = 0; j < dou; j++){
                    OpenRedPack openRedPack;
                    do {
                        int d = 1 + (int)(Math.random()*(index + 1 - 1));
                        openRedPack = tc.openRedPackDao.queryByCondition(d);
                        if(openRedPack.getPrizeMoney().doubleValue() == 0){
                            break;
                        }
                    } while (openRedPack.getPrizeMoney().doubleValue() > 0);
                    if(openRedPack != null){
                        openRedPack.setPrizeMoney(bigDecimal369);
                        openRedPack.setUpdateTime(new Date());
                        tc.openRedPackDao.update(openRedPack);
                    }
                }
                branch = prizePool.getOneThird().subtract(bigDecimal369.multiply(new BigDecimal(dou)));
                if(branch.doubleValue() > 0){
                    int x = prizePool.getRedPackQuantity() - dou; // 67
                    List<Double> al = RandomNumberUtil.splitRedPacket(branch.doubleValue(), x, 60, 180);
                    if(al.size() > 0){
                        System.err.println("开始分普通红包 > 10 -------------------------------------------------------------");
                        List<OpenRedPack> openRedPackList1 = tc.openRedPackDao.queryByToday1(time);
                        for(int y = 0; y < openRedPackList1.size(); y++){
                            OpenRedPack open = openRedPackList1.get(y);
                            open.setPrizeMoney(new BigDecimal(al.get(y)));
                            open.setUpdateTime(new Date());
                            tc.openRedPackDao.update(open);
                        }
                    }
                }
            }else if(index < 10){
                // 直接按60-180分配
                if(prizePool.getOneThird().doubleValue() > 0){
                    int x = prizePool.getRedPackQuantity();
                    List<Double> al = RandomNumberUtil.splitRedPacket(prizePool.getOneThird().doubleValue(), x, 60, 180);
                    if(al.size() > 0){
                        System.err.println("开始分普通红包 < 10 -------------------------------------------------------------");
                        List<OpenRedPack> openRedPackList1 = tc.openRedPackDao.queryByToday1(time);
                        for(int y = 0; y < openRedPackList1.size(); y++){
                            OpenRedPack open = openRedPackList1.get(y);
                            open.setPrizeMoney(new BigDecimal(al.get(y)));
                            open.setUpdateTime(new Date());
                            tc.openRedPackDao.update(open);
                        }
                    }
                }
            }
            System.err.println("02:15 内容：分配每个红包中奖金额完毕 -------------------------------------------------------------");
        }else{
            System.err.println("02:15 内容：今日无需分配红包 -------------------------------------------------------------");
        }
        System.err.println("02:15 定时任务结束 -------------------------------------------------------------");
    }


    // 每天凌晨20分 清空个人红包券信息，然后生成新的奖金池
    //@Scheduled(cron = "0 35 2 * * ?")
    public void TimingBusiness4(){
        System.err.println("02:35 定时任务开始 -------------------------------------------------------------");
        List<Account> accountList = tc.accountDao.queryAccountByRedPack();
        if(accountList.size() > 0){
            for(Account account : accountList){
                account.setRedPacket(0);
                tc.accountDao.update(account);
            }
            System.err.println("02:35 内容：个人红包券已清零 -------------------------------------------------------------");
        }
        PrizePool newPrizePool = new PrizePool();
        newPrizePool.setUpdateTime(new Date());
        BigDecimal bd = new BigDecimal(0);
        newPrizePool.setSurplusMoney(bd);
        newPrizePool.setOneThird(bd);
        newPrizePool.setTotalMoney(bd);
        newPrizePool.setRedPackQuantity(0);
        newPrizePool.setPayMoney(bd);
        tc.prizePoolDao.save(newPrizePool);
        System.err.println("02:35 内容：今日新奖金池已生成 -------------------------------------------------------------");
        System.err.println("02:35 定时任务结束 -------------------------------------------------------------");
    }


    // 上架礼包
    //@Scheduled(cron = "0 59 2 * * ?")
    public void TimingBusiness5() {
        System.err.println("02:59 定时任务开始 -------------------------------------------------------------");
        List<Product> productList = tc.productDao.queryProductByType3_1();
        if (productList.size() > 0) {
            for (Product product : productList) {
                product.setIsShelf("1");
                tc.productDao.update(product);
            }
            System.err.println("02:59 内容：礼包已上架 -------------------------------------------------------------");
        }
        System.err.println("02:59 定时任务结束 -------------------------------------------------------------");
    }


    // 每天早上九点，展示红包，提前1分钟开启
    //@Scheduled(cron = "0 59 8 * * ?")
    public void TimingBusiness6() {
        System.err.println("9:00 定时任务开始 -------------------------------------------------------------");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        List<OpenRedPack> openRedPackList = tc.openRedPackDao.queryByToday(date);
        if(openRedPackList.size() > 0){
            for(OpenRedPack openRedPack : openRedPackList){
                openRedPack.setIsShow("1");
                tc.openRedPackDao.update(openRedPack);
            }
            System.err.println("9:00 内容：展示所有人的红包 -------------------------------------------------------------");
        }
        System.err.println("9:00 定时任务结束 -------------------------------------------------------------");
    }


    // 每个月1号凌晨2:40 分配管理分红
    //@Scheduled(cron = "0 40 2 1 * ?")
    public void TimingBusiness7() throws ParseException {
        System.err.println("02:40 定时任务开始 -------------------------------------------------------------");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String time = sdf.format(new Date());
        System.err.println("02:40 内容：本月时间是 -------------------------------------------------------------" + time);
        //1代表为一个月  正的往前  负的为后
        Date date11 = sdf.parse(time);
        Date date = stepMonth(date11, -1);
        String dateT = sdf.format(date);
        System.err.println("02:40 内容：前一个月时间是 -------------------------------------------------------------" + dateT);

        // 当月奖金池总业绩
        BigDecimal totalMoney = tc.prizePoolDao.queryByTotalPayMoney(dateT);
        System.err.println("02:40 内容：前一个月总金额为：-------------------------------------------------------------" + totalMoney);
        if(totalMoney.doubleValue() > 0){
            Date dateTime = new Date();
            // 专员的3%利润
            BigDecimal profit6 = totalMoney.multiply(new BigDecimal(0.03)).setScale(2, BigDecimal.ROUND_UP);
            // 专员的总业绩
            BigDecimal total6 = tc.customerDao.queryByTotalAchievement(6);
            if(total6.doubleValue() > 0){
                List<Customer> list6 = tc.customerDao.queryByLevelCopyId(6);
                if(list6.size() > 0){
                    for(Customer c6 : list6){
                        // 专员个人业绩除以总业绩
                        BigDecimal div = c6.getAchievement().divide(total6, 4, BigDecimal.ROUND_HALF_UP);
                        BigDecimal mul = div.multiply(profit6).setScale(2, BigDecimal.ROUND_UP);
                        Account account = tc.accountDao.queryAccountByUserId(c6.getId());
                        account.setVoucherBalance(account.getVoucherBalance().add(mul));
                        tc.accountDao.update(account);
                        AccountRecord ar1 = new AccountRecord();
                        ar1.setIntoUserId(c6.getId());
                        ar1.setUpdateTime(dateTime);
                        ar1.setType("0");
                        ar1.setAmount(mul);
                        ar1.setNowAmount(account.getVoucherBalance());
                        ar1.setSign("1");
                        ar1.setInfo("共享专区每月(专员)管理分红，您获得：" + mul);
                        accountRecordDao.save(ar1);
                        System.err.println("02:40 内容：为每个专员分红----------------------- " + c6.getAgentCode() + " = " + mul);
                    }
                }
            }

            // 主管的2%利润
            BigDecimal profit7 = totalMoney.multiply(new BigDecimal(0.02)).setScale(2, BigDecimal.ROUND_UP);
            // 主管的总业绩
            BigDecimal total7 = tc.customerDao.queryByTotalAchievement(7);
            if(total7.doubleValue() > 0){
                List<Customer> list7 = tc.customerDao.queryByLevelCopyId(7);
                if(list7.size() > 0){
                    for(Customer c7 : list7){
                        // 主管个人业绩除以总业绩
                        BigDecimal div = c7.getAchievement().divide(total7, 4, BigDecimal.ROUND_HALF_UP);
                        BigDecimal mul = div.multiply(profit7).setScale(2, BigDecimal.ROUND_UP);
                        Account account = tc.accountDao.queryAccountByUserId(c7.getId());
                        account.setVoucherBalance(account.getVoucherBalance().add(mul));
                        tc.accountDao.update(account);
                        AccountRecord ar1 = new AccountRecord();
                        ar1.setIntoUserId(c7.getId());
                        ar1.setUpdateTime(dateTime);
                        ar1.setType("0");
                        ar1.setAmount(mul);
                        ar1.setNowAmount(account.getVoucherBalance());
                        ar1.setSign("1");
                        ar1.setInfo("共享专区每月(主管)管理分红，您获得：" + mul);
                        accountRecordDao.save(ar1);
                        System.err.println("02:40 内容：为每个主管分红----------------------- " + c7.getAgentCode() + " = " + mul);
                    }
                }
            }

            // 经理的1%利润
            BigDecimal profit8 = totalMoney.multiply(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_UP);
            // 经理的总业绩
            BigDecimal total8 = tc.customerDao.queryByTotalAchievement(8);
            if(total8.doubleValue() > 0){
                List<Customer> list8 = tc.customerDao.queryByLevelCopyId(8);
                if(list8.size() > 0){
                    for(Customer c8 : list8){
                        // 经理个人业绩除以总业绩
                        BigDecimal div = c8.getAchievement().divide(total8, 4, BigDecimal.ROUND_HALF_UP);
                        BigDecimal mul = div.multiply(profit8).setScale(2, BigDecimal.ROUND_UP);
                        Account account = tc.accountDao.queryAccountByUserId(c8.getId());
                        account.setVoucherBalance(account.getVoucherBalance().add(mul));
                        tc.accountDao.update(account);
                        AccountRecord ar1 = new AccountRecord();
                        ar1.setIntoUserId(c8.getId());
                        ar1.setUpdateTime(dateTime);
                        ar1.setType("0");
                        ar1.setAmount(mul);
                        ar1.setNowAmount(account.getVoucherBalance());
                        ar1.setSign("1");
                        ar1.setInfo("共享专区每月(经理)管理分红，您获得：" + mul);
                        accountRecordDao.save(ar1);
                        System.err.println("02:40 内容：为每个经理分红----------------------- " + c8.getAgentCode() + " = " + mul);
                    }
                }
            }

            // 总监的0.5%利润
            BigDecimal profit9 = totalMoney.multiply(new BigDecimal(0.005)).setScale(2, BigDecimal.ROUND_UP);
            // 总监的总业绩
            BigDecimal total9 = tc.customerDao.queryByTotalAchievement(9);
            if(total9.doubleValue() > 0){
                List<Customer> list9 = tc.customerDao.queryByLevelCopyId(9);
                if(list9.size() > 0){
                    for(Customer c9 : list9){
                        // 总监个人业绩除以总业绩
                        BigDecimal div = c9.getAchievement().divide(total9, 4, BigDecimal.ROUND_HALF_UP);
                        BigDecimal mul = div.multiply(profit9).setScale(2, BigDecimal.ROUND_UP);
                        Account account = tc.accountDao.queryAccountByUserId(c9.getId());
                        account.setVoucherBalance(account.getVoucherBalance().add(mul));
                        tc.accountDao.update(account);
                        AccountRecord ar1 = new AccountRecord();
                        ar1.setIntoUserId(c9.getId());
                        ar1.setUpdateTime(dateTime);
                        ar1.setType("0");
                        ar1.setAmount(mul);
                        ar1.setNowAmount(account.getVoucherBalance());
                        ar1.setSign("1");
                        ar1.setInfo("共享专区每月(总监)管理分红，您获得：" + mul);
                        accountRecordDao.save(ar1);
                        System.err.println("02:40 内容：为每个总监分红----------------------- " + c9.getAgentCode() + " = " + mul);
                    }
                }
            }

            // 总裁的0.5%利润
            BigDecimal profit10 = totalMoney.multiply(new BigDecimal(0.005)).setScale(2, BigDecimal.ROUND_UP);
            // 总裁的总业绩
            BigDecimal total10 = tc.customerDao.queryByTotalAchievement(10);
            if(total10.doubleValue() > 0){
                List<Customer> list10 = tc.customerDao.queryByLevelCopyId(10);
                if(list10.size() > 0){
                    for(Customer c10 : list10){
                        // 总裁个人业绩除以总业绩
                        BigDecimal div = c10.getAchievement().divide(total10, 4, BigDecimal.ROUND_HALF_UP);
                        BigDecimal mul = div.multiply(profit10).setScale(2, BigDecimal.ROUND_UP);
                        Account account = tc.accountDao.queryAccountByUserId(c10.getId());
                        account.setVoucherBalance(account.getVoucherBalance().add(mul));
                        tc.accountDao.update(account);
                        AccountRecord ar1 = new AccountRecord();
                        ar1.setIntoUserId(c10.getId());
                        ar1.setUpdateTime(dateTime);
                        ar1.setType("0");
                        ar1.setAmount(mul);
                        ar1.setNowAmount(account.getVoucherBalance());
                        ar1.setSign("1");
                        ar1.setInfo("共享专区每月(总裁)管理分红，您获得：" + mul);
                        accountRecordDao.save(ar1);
                        System.err.println("02:40 内容：为每个总裁分红----------------------- " + c10.getAgentCode() + " = " + mul);
                    }
                }
            }

        }

        // 清空所有人当月业绩，重新累计
        List<Customer> customerList = tc.customerDao.queryByEmptyAchievement();
        if(customerList.size() > 0){
            for(Customer cu : customerList){
                cu.setAchievement(new BigDecimal(0));
                tc.customerDao.update(cu);
            }
            System.err.println("02:40 内容：所有人业绩都已清空----------------------- ");
        }
        System.err.println("02:40 定时任务结束 -------------------------------------------------------------");
    }


    // 每天晚上04:30 查询今日是否有需要返回一个代售的
    //@Scheduled(cron = "0 30 4 * * ?")
    public void TimingBusiness9() {
        System.err.println("04:30 定时任务开始 -------------------------------------------------------------");

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String time = df.format(new Date());

        // 查询今日是否有需要返回一个代售的
        List<CollageInRecord3> cir3List = tc.collageInRecord3Dao.queryByIsNeedInspect(time);
        if(cir3List.size() > 0){
            for(CollageInRecord3 cir3 : cir3List){

                cir3.setStatus("1"); // 已返还

                System.err.println("今日返还订单 -----------------------------开始-------------------------------- " + cir3.getOrderNo());

                // 退钱
                Account acc3 = tc.accountDao.queryAccountByUserId(cir3.getUserId());
                acc3.setVoucherBalance(acc3.getVoucherBalance().add(cir3.getPayAmount()));
                tc.accountDao.update(acc3);

                AccountRecord ar = new AccountRecord();
                ar.setIntoUserId(cir3.getUserId());
                ar.setUpdateTime(new Date());
                ar.setType("0");
                ar.setAmount(cir3.getPayAmount());
                ar.setNowAmount(acc3.getVoucherBalance());
                ar.setSign("1");
                ar.setOrderNo(cir3.getOrderNo());
                // TODO 将12改成3
                ar.setInfo(tc.customerDao.queryById(cir3.getUserId()).getAgentCode() + "参团成功! 累计抽中已满三次, 本月返还一个代售：" + cir3.getPayAmount());
                tc.accountRecordDao.save(ar);

                System.err.println("该订单金额 ------------------------------------------------------------- " + cir3.getPayAmount());

                Orders o = tc.ordersDao.queryByOrderNo(cir3.getOrderNo());

                // 修改订单信息
                o.setDealStatus("4"); // 交易关闭
                o.setRefundStatus("2"); // 退款成功
                o.setPayStatus("3"); // 已退款
                tc.ordersDao.update(o);

                System.err.println("该订单状态已修改 -------------------------------------------------------------");

                tc.collageInRecord3Dao.update(cir3);

                System.err.println("当前代售已返还 -------------------------------------------------------------");

                // 送共享券
                UserCoupon userCoupon = tc.userCouponDao.queryByUserIdAndCouponId(cir3.getUserId(), 2);
                if(userCoupon != null){
                    userCoupon.setQuantity(userCoupon.getQuantity() + 1);
                    userCoupon.setUpdateTime(new Date());
                    tc.userCouponDao.update(userCoupon);

                    System.err.println("共享券成功送出 old -------------------------------------------------------------");

                    AccountRecord ar1 = new AccountRecord();
                    ar1.setIntoUserId(cir3.getUserId());
                    ar1.setUpdateTime(new Date());
                    ar1.setType("6");
                    ar1.setAmount(new BigDecimal(1));
                    ar1.setNowAmount(new BigDecimal(userCoupon.getQuantity()));
                    ar1.setSign("1");
                    // TODO 将12改成3
                    ar1.setInfo(tc.customerDao.queryById(cir3.getUserId()).getAgentCode() + "参团成功! 累计抽中已满三次, 本月返还代售时赠送共享券：" + 1);
                    tc.accountRecordDao.save(ar1);
                }else{
                    UserCoupon newUc = new UserCoupon();
                    newUc.setQuantity(1);
                    newUc.setUpdateTime(new Date());
                    newUc.setCouponId(2);
                    newUc.setUserId(cir3.getUserId());
                    tc.userCouponDao.save(newUc);

                    System.err.println("共享券成功送出 new -------------------------------------------------------------");

                    AccountRecord ar2 = new AccountRecord();
                    ar2.setIntoUserId(cir3.getUserId());
                    ar2.setUpdateTime(new Date());
                    ar2.setType("6");
                    ar2.setAmount(new BigDecimal(1));
                    ar2.setNowAmount(new BigDecimal(newUc.getQuantity()));
                    ar2.setSign("1");
                    // TODO 将12改成3
                    ar2.setInfo(tc.customerDao.queryById(cir3.getUserId()).getAgentCode() + "参团成功! 累计抽中已满三次, 本月返还代售时赠送共享券：" + 1);
                    tc.accountRecordDao.save(ar2);
                }

                System.err.println("今日返还订单 -----------------------------结束-------------------------------- " + cir3.getOrderNo());
            }
        }
        System.err.println("今日没有要返还的代售 -------------------------------------------------------------");
        System.err.println("04:30 定时任务结束 -------------------------------------------------------------");
    }


    // 每隔5秒将OrdersCopy待转移的订单，重新下单
    //@Scheduled(cron="0/5 * *  * * ? ")
    public void TimingBusiness8() throws ParseException {
        //System.err.println("每隔3秒 定时任务开始 -------------------------------------------------------------");
        List<OrdersCopy> ordersCopyList = tc.ordersCopyDao.queryAll();
        if(ordersCopyList.size() > 0){
            OrdersCopy oc = ordersCopyList.get(0);
            // 商户订单号
            String outTradeNo = "BEE" + Units.getOrderIdByTime().replaceAll("-", "");
            System.err.println("创建新订单号 -------------------------------------------------------------" + outTradeNo);
            // 交易号
            String tradeNo = "JYH" + Units.getOrderIdByTime().replaceAll("-", "");
            boolean isSpellSelect = true;
            CollageMode cm = tc.collageModeDao.queryById(oc.getCollageModeId());
            Product product = tc.productDao.queryById(oc.getProductId());

            // 修改老订单
            Orders orders = tc.ordersDao.queryByOrderNo(oc.getOrderNo());
            orders.setDealStatus("4"); // 交易关闭
            orders.setRefundStatus("5"); // 交易关闭
            orders.setPayStatus("4"); // 交易关闭
            orders.setRemarks(oc.getOrderNo() + " 转移到了 " + outTradeNo);
            tc.ordersDao.update(orders);
            System.err.println("修改老订单 交易关闭 -------------------------------------------------------------");

            // 重新下单
            System.err.println("重新下单开始 -------------------------------------------------------------");
            tc.ordersCopyService.handleCollage(cm, product, oc.getOrderPersonId(), oc.getTotalAmount(), oc.getPayAmount(),
                    oc.getConsigneeName(), oc.getAddress(), oc.getConsigneePhone(), oc.getFreight(), oc.getCouponAmount(),
                    oc.getCouponQuantity(), oc.getSkuId(), isSpellSelect, oc.getCouponId(), outTradeNo, tradeNo);
            System.err.println("重新下单结束 -------------------------------------------------------------");

            // 当前订单移出
            tc.ordersCopyDao.delete(oc.getId());
            System.err.println("当前订单移出 OrdersCopy -------------------------------------------------------------");

        }

        // 20人团
        List<OrdersCopy30> oc30List = tc.ordersCopy30Dao.queryAll();
        if(oc30List.size() > 0){

            OrdersCopy30 oc30 = oc30List.get(0);
            // 商户订单号
            String outTradeNo = "BEE" + Units.getOrderIdByTime().replaceAll("-", "");
            // 交易号
            String tradeNo = "JYH" + Units.getOrderIdByTime().replaceAll("-", "");

            // 修改老订单
            Orders orders = tc.ordersDao.queryByOrderNo(oc30.getOrderNo());
            orders.setDealStatus("4"); // 交易关闭
            orders.setRefundStatus("5"); // 交易关闭
            orders.setPayStatus("4"); // 交易关闭
            orders.setRemarks(oc30.getOrderNo() + " 转移到了 " + outTradeNo);
            tc.ordersDao.update(orders);
            System.err.println("重新下单开始 ------------------------------- " + oc30.getOrderNo());
            // 重新下单
            tc.ordersCopy30Service.handleAgainOrders(oc30.getCollageModeId(), oc30.getProductId(), oc30.getOrderPersonId(), oc30.getPayAmount(),
                    oc30.getConsigneeName(), oc30.getAddress(), oc30.getConsigneePhone(), oc30.getFreight(), oc30.getSkuId(), outTradeNo, tradeNo);
            System.err.println("重新下单结束 -------------------------------------------------------------");
            // 当前订单移出
            tc.ordersCopy30Dao.delete(oc30.getId());
        }

        //System.err.println("每隔3秒 定时任务结束 -------------------------------------------------------------");
    }


    // 每天 上午十点整，下午三点整，晚上八点整，处理极乐桑巴团 正式
    //@Scheduled(cron = "0 0 10,15,20 * * ?") // 线上为 10,15,20
    public void TimingBusiness10(){
        System.err.println("每天 上午十点整，下午三点整，晚上八点，处理极乐桑巴团 正式 -------------------开始---------正式---------------------------------");
        // 成功正在等待中的团
        List<ModeProduct> mpList = tc.modeProductDao.queryByIsWaitFor();
        if(mpList.size() > 0){
            System.err.println("成功正在等待中的团 ----------------------------成功,等待处理中的---------------------------------" + mpList.size());
            for(ModeProduct mp : mpList){
                // 获取每个团所有的订单
                List<Orders> ordersList = tc.ordersDao.queryOrdersByCollageProductId(mp.getId());
                int ordersSize = ordersList.size();
                if(ordersSize > 0){
                    Random random = new Random();
                    int n = random.nextInt(ordersSize); // 随机抽中一条数据
                    for(int u = 0; u < ordersSize; u++) {
                        Orders o = ordersList.get(u);
                        Product product = tc.productDao.queryById(o.getProductId());
                        Customer customer = tc.customerDao.queryById(o.getOrderPersonId());
                        BigDecimal couponAmount = null;
                        int quantity1 = 0;
                        if (o.getCouponAmount() == null) {
                            couponAmount = new BigDecimal(0);
                        } else {
                            couponAmount = o.getCouponAmount();
                        }
                        if (couponAmount.doubleValue() > 0) {
                            quantity1 = (int) (couponAmount.doubleValue() / 9.9);
                        }
                        if (u == n) {
                            // 添加销量
                            product.setSales(product.getSales() + 1);
                            tc.productDao.update(product);

                            // 中奖
                            if (o.getCollageModeId() == 2) { // 十八人团
                                o.setDealStatus("1"); // 待发货
                            } else if (o.getCollageModeId() == 3) { // 九人团
                                o.setDealStatus("7"); // 代售
                            } else if (o.getCollageModeId() == 4) { // 溜溜溜
                                o.setDealStatus("1"); // 待发货
                            } else if (o.getCollageModeId() == 20) { // 速度与激情
                                o.setDealStatus("1"); // 待发货
                            }
                            tc.ordersDao.update(o);

                            Account acc = tc.accountDao.queryAccountByUserId(o.getOrderPersonId());
                            // 送3个极乐值
                            acc.setBliss(acc.getBliss() + 3);
                            // 送等额的积分
                            int give = (int) Math.round(o.getPayAmount().doubleValue());
                            acc.setIntegral(acc.getIntegral() + give);
                            // 拼中也送红包余额
                            acc.setVoucherBalance(acc.getVoucherBalance().add(product.getVoucher1()));
                            tc.accountDao.update(acc);

                            AccountRecord ar3 = new AccountRecord();
                            ar3.setIntoUserId(o.getOrderPersonId());
                            ar3.setOrderNo(o.getOrderNo());
                            ar3.setType("2");
                            ar3.setSign("1");
                            ar3.setUpdateTime(new Date());
                            ar3.setAmount(new BigDecimal(3));
                            ar3.setNowAmount(new BigDecimal(acc.getBliss()));
                            ar3.setInfo(customer.getAgentCode() + "参团成功! 拼中产品获得极乐值：" + 3);
                            tc.accountRecordDao.save(ar3);

                            AccountRecord ar3_1 = new AccountRecord();
                            ar3_1.setIntoUserId(o.getOrderPersonId());
                            ar3_1.setOrderNo(o.getOrderNo());
                            ar3_1.setType("1");
                            ar3_1.setSign("1");
                            ar3_1.setUpdateTime(new Date());
                            ar3_1.setAmount(new BigDecimal(give));
                            ar3_1.setNowAmount(new BigDecimal(acc.getIntegral()));
                            ar3_1.setInfo(customer.getAgentCode() + "参团成功! 拼中产品获得积分：" + give);
                            tc.accountRecordDao.save(ar3_1);

                            AccountRecord ar3_2 = new AccountRecord();
                            ar3_2.setIntoUserId(o.getOrderPersonId());
                            ar3_2.setUpdateTime(new Date());
                            ar3_2.setType("0");
                            ar3_2.setAmount(product.getVoucher1());
                            ar3_2.setNowAmount(acc.getVoucherBalance());
                            ar3_2.setSign("1");
                            ar3_2.setOrderNo(o.getOrderNo());
                            ar3_2.setInfo(customer.getAgentCode() + "参团成功! 拼中产品获得红包余额：" + product.getVoucher1());
                            tc.accountRecordDao.save(ar3_2);

                            // 处理参与拼团后的利润分配
                            tc.ordersCopyService.handleCollageProfit(o.getOrderPersonId(), product.getVoucher1(), o.getOrderNo());

                            // 极乐桑巴
                            if (o.getCollageModeId() == 2) {
                                // 判断当前产品是否满六次，执行全额退款
                                CollageInRecord2 cir2 = tc.collageInRecord2Dao.queryCir2ByIds(o.getCollageModeId(), o.getProductId(), o.getOrderPersonId());
                                if (cir2 == null) {
                                    // 第一次中当前产品
                                    CollageInRecord2 newCir2 = new CollageInRecord2();
                                    newCir2.setModeId(o.getCollageModeId());
                                    newCir2.setProductId(o.getProductId());
                                    newCir2.setUserId(o.getOrderPersonId());
                                    newCir2.setStatus("0");
                                    newCir2.setTotalQuantity(1);
                                    newCir2.setPayAmount(o.getPayAmount());
                                    newCir2.setUpdateTime(new Date());
                                    tc.collageInRecord2Dao.save(newCir2);
                                } else {
                                    // TODO 将6改成3
                                    if (cir2.getTotalQuantity() < 3) {
                                        // 继续累积
                                        cir2.setPayAmount(cir2.getPayAmount().add(o.getPayAmount()));
                                        cir2.setTotalQuantity(cir2.getTotalQuantity() + 1);
                                        cir2.setUpdateTime(new Date());
                                        tc.collageInRecord2Dao.update(cir2);
                                    } else {
                                        // 满足六次，退钱六次的钱
                                        cir2.setStatus("1");
                                        cir2.setUpdateTime(new Date());
                                        tc.collageInRecord2Dao.update(cir2);

                                        Account accCir = tc.accountDao.queryAccountByUserId(o.getOrderPersonId());
                                        accCir.setVoucherBalance(accCir.getVoucherBalance().add(cir2.getPayAmount()));
                                        tc.accountDao.update(accCir);

                                        AccountRecord arCir = new AccountRecord();
                                        arCir.setIntoUserId(o.getOrderPersonId());
                                        arCir.setUpdateTime(new Date());
                                        arCir.setType("0");
                                        arCir.setAmount(cir2.getPayAmount());
                                        arCir.setNowAmount(accCir.getVoucherBalance());
                                        arCir.setSign("1");
                                        arCir.setOrderNo(o.getOrderNo());
                                        // TODO 将6改成3
                                        arCir.setInfo(customer.getAgentCode() + "参团成功! 累积拼中产品三次,获得广告补贴为：" + cir2.getPayAmount());
                                        tc.accountRecordDao.save(arCir);

                                        // 送共享券
                                        UserCoupon userCoupon = tc.userCouponDao.queryByUserIdAndCouponId(cir2.getUserId(), 2);
                                        if (userCoupon != null) {
                                            userCoupon.setQuantity(userCoupon.getQuantity() + 1);
                                            userCoupon.setUpdateTime(new Date());
                                            tc.userCouponDao.update(userCoupon);

                                            AccountRecord ar1 = new AccountRecord();
                                            ar1.setIntoUserId(cir2.getUserId());
                                            ar1.setUpdateTime(new Date());
                                            ar1.setType("6");
                                            ar1.setAmount(new BigDecimal(1));
                                            ar1.setNowAmount(new BigDecimal(userCoupon.getQuantity()));
                                            ar1.setSign("1");
                                            // TODO 将6改成3
                                            ar1.setInfo(tc.customerDao.queryById(cir2.getUserId()).getAgentCode() + "参团成功! 累积拼中产品三次,退款总金额并赠送共享券：" + 1);
                                            tc.accountRecordDao.save(ar1);
                                        } else {
                                            UserCoupon newUc = new UserCoupon();
                                            newUc.setQuantity(1);
                                            newUc.setUpdateTime(new Date());
                                            newUc.setCouponId(2);
                                            newUc.setUserId(cir2.getUserId());
                                            tc.userCouponDao.save(newUc);

                                            AccountRecord ar2 = new AccountRecord();
                                            ar2.setIntoUserId(cir2.getUserId());
                                            ar2.setUpdateTime(new Date());
                                            ar2.setType("6");
                                            ar2.setAmount(new BigDecimal(1));
                                            ar2.setNowAmount(new BigDecimal(newUc.getQuantity()));
                                            ar2.setSign("1");
                                            // TODO 将6改成3
                                            ar2.setInfo(tc.customerDao.queryById(cir2.getUserId()).getAgentCode() + "参团成功! 累积拼中产品三次,退款总金额并赠送共享券：" + 1);
                                            tc.accountRecordDao.save(ar2);
                                        }

                                        // 存下第七次
                                        CollageInRecord2 newCir7 = new CollageInRecord2();
                                        newCir7.setModeId(o.getCollageModeId());
                                        newCir7.setProductId(o.getProductId());
                                        newCir7.setUserId(o.getOrderPersonId());
                                        newCir7.setStatus("0");
                                        newCir7.setTotalQuantity(1);
                                        newCir7.setPayAmount(o.getPayAmount());
                                        newCir7.setUpdateTime(new Date());
                                        tc.collageInRecord2Dao.save(newCir7);
                                    }
                                }

                                // 判断当前拼团模式中是否存在满足六次的，有就执行退款
                                // TODO 将6改成3
                                CollageInRecord2 cir2_1 = tc.collageInRecord2Dao.queryCir2ByIds_1(o.getCollageModeId(), o.getProductId(), o.getOrderPersonId());
                                if (cir2_1 != null) {
                                    // 满足六次，退钱六次的钱
                                    cir2_1.setStatus("1");
                                    cir2_1.setUpdateTime(new Date());
                                    tc.collageInRecord2Dao.update(cir2_1);

                                    Account accCir_1 = tc.accountDao.queryAccountByUserId(o.getOrderPersonId());
                                    accCir_1.setVoucherBalance(accCir_1.getVoucherBalance().add(cir2_1.getPayAmount()));
                                    tc.accountDao.update(accCir_1);

                                    AccountRecord arCir_1 = new AccountRecord();
                                    arCir_1.setIntoUserId(o.getOrderPersonId());
                                    arCir_1.setUpdateTime(new Date());
                                    arCir_1.setType("0");
                                    arCir_1.setAmount(cir2_1.getPayAmount());
                                    arCir_1.setNowAmount(accCir_1.getVoucherBalance());
                                    arCir_1.setSign("1");
                                    arCir_1.setOrderNo(o.getOrderNo());
                                    // TODO 将6改成3
                                    arCir_1.setInfo(customer.getAgentCode() + "参团成功! 累积拼中产品三次,获得广告补贴为：" + cir2_1.getPayAmount());
                                    tc.accountRecordDao.save(arCir_1);

                                    // 送共享券
                                    UserCoupon userCoupon = tc.userCouponDao.queryByUserIdAndCouponId(cir2_1.getUserId(), 2);
                                    if (userCoupon != null) {
                                        userCoupon.setQuantity(userCoupon.getQuantity() + 1);
                                        userCoupon.setUpdateTime(new Date());
                                        tc.userCouponDao.update(userCoupon);

                                        AccountRecord ar1 = new AccountRecord();
                                        ar1.setIntoUserId(cir2_1.getUserId());
                                        ar1.setUpdateTime(new Date());
                                        ar1.setType("6");
                                        ar1.setAmount(new BigDecimal(1));
                                        ar1.setNowAmount(new BigDecimal(userCoupon.getQuantity()));
                                        ar1.setSign("1");
                                        // TODO 将6改成3
                                        ar1.setInfo(tc.customerDao.queryById(cir2_1.getUserId()).getAgentCode() + "参团成功! 累积拼中产品三次,退款总金额并赠送共享券：" + 1);
                                        tc.accountRecordDao.save(ar1);
                                    } else {
                                        UserCoupon newUc = new UserCoupon();
                                        newUc.setQuantity(1);
                                        newUc.setUpdateTime(new Date());
                                        newUc.setCouponId(2);
                                        newUc.setUserId(cir2_1.getUserId());
                                        tc.userCouponDao.save(newUc);

                                        AccountRecord ar2 = new AccountRecord();
                                        ar2.setIntoUserId(cir2_1.getUserId());
                                        ar2.setUpdateTime(new Date());
                                        ar2.setType("6");
                                        ar2.setAmount(new BigDecimal(1));
                                        ar2.setNowAmount(new BigDecimal(newUc.getQuantity()));
                                        ar2.setSign("1");
                                        // TODO 将6改成3
                                        ar2.setInfo(tc.customerDao.queryById(cir2_1.getUserId()).getAgentCode() + "参团成功! 累积拼中产品三次,退款总金额并赠送共享券：" + 1);
                                        tc.accountRecordDao.save(ar2);
                                    }
                                }

                            }

                        } else {
                            // 是否退款
                            if ("1".equals(o.getLastCollage())) {

                                // 当前订单转移到 OrdersCopy
                                OrdersCopy oc = new OrdersCopy();
                                oc.setId(o.getId());
                                oc.setOrderNo(o.getOrderNo());
                                oc.setTradeNo(o.getTradeNo());
                                oc.setTotalAmount(o.getTotalAmount());
                                oc.setFreight(o.getFreight());
                                oc.setCollageModeId(o.getCollageModeId());
                                oc.setOrderPersonId(o.getOrderPersonId());
                                oc.setLastCollage(o.getLastCollage());
                                oc.setConsigneeName(o.getConsigneeName());
                                oc.setConsigneePhone(o.getConsigneePhone());
                                oc.setAddress(o.getAddress());
                                oc.setUpdateTime(new Date());
                                oc.setPayAmount(o.getPayAmount());
                                oc.setCouponAmount(o.getCouponAmount());
                                oc.setModeProductId(o.getModeProductId());
                                oc.setProductId(o.getProductId());
                                oc.setCouponId(1);
                                oc.setSkuId(tc.orderDetailsDao.queryByOrderNo(o.getOrderNo()).getRelationId());

                                if (quantity1 > 0) {
                                    oc.setCouponQuantity(quantity1);
                                } else {
                                    oc.setCouponQuantity(0);
                                }
                                tc.ordersCopyDao.save(oc);

                                // 给个红包
//                                Account accN = tc.accountDao.queryAccountByUserId(o.getOrderPersonId());
//                                accN.setVoucherBalance(accN.getVoucherBalance().add(product.getVoucher()));
//                                tc.accountDao.update(accN);
//
//                                AccountRecord ar1_2 = new AccountRecord();
//                                ar1_2.setIntoUserId(o.getOrderPersonId());
//                                ar1_2.setUpdateTime(new Date());
//                                ar1_2.setType("0");
//                                ar1_2.setAmount(product.getVoucher());
//                                ar1_2.setNowAmount(accN.getVoucherBalance());
//                                ar1_2.setSign("1");
//                                ar1_2.setOrderNo(o.getOrderNo());
//                                ar1_2.setInfo(customer.getAgentCode() + "参团成功! 未抽中,送您余额：" + product.getVoucher());
//                                tc.accountRecordDao.save(ar1_2);

                                FailRedPack frp = new FailRedPack();
                                frp.setOrderNo(o.getOrderNo());
                                frp.setPrizeMoney(product.getVoucher());
                                frp.setStatus("2"); // 未开红包
                                frp.setUpdateTime(new Date());
                                frp.setUserId(o.getOrderPersonId());
                                frp.setMode("0");
                                tc.failRedPackDao.save(frp);

                                // 处理参与拼团后的利润分配
                                // tc.ordersCopyService.handleCollageProfit(o.getOrderPersonId(), product.getVoucher(), o.getOrderNo());

                            } else {
                                // 执行退款(退到余额)退券 把每个人领取到的红包进行分润
                                o.setDealStatus("4"); // 交易关闭
                                o.setRefundStatus("2"); // 退款成功
                                o.setPayStatus("3"); // 已退款
                                tc.ordersDao.update(o);

                                Account acc = tc.accountDao.queryAccountByUserId(o.getOrderPersonId());
                                acc.setVoucherBalance(acc.getVoucherBalance().add(o.getPayAmount()));
                                tc.accountDao.update(acc);

                                AccountRecord ar1_1 = new AccountRecord();
                                ar1_1.setIntoUserId(o.getOrderPersonId());
                                ar1_1.setUpdateTime(new Date());
                                ar1_1.setType("0");
                                ar1_1.setAmount(o.getPayAmount());
                                ar1_1.setNowAmount(acc.getVoucherBalance());
                                ar1_1.setSign("1");
                                ar1_1.setOrderNo(o.getOrderNo());
                                ar1_1.setInfo(customer.getAgentCode() + "参团成功! 未抽中,支付金额退回至余额：" + o.getPayAmount());
                                tc.accountRecordDao.save(ar1_1);

                                if (quantity1 > 0) {
                                    UserCoupon uc = tc.userCouponDao.queryByUserIdAndCouponId(o.getOrderPersonId(), 1);
                                    if (uc != null) {
                                        uc.setQuantity(uc.getQuantity() + quantity1);
                                        tc.userCouponDao.update(uc);

                                        AccountRecord ar2 = new AccountRecord();
                                        ar2.setIntoUserId(o.getOrderPersonId());
                                        ar2.setUpdateTime(new Date());
                                        ar2.setType("5"); // 拼团券
                                        ar2.setAmount(new BigDecimal(quantity1));
                                        ar2.setNowAmount(new BigDecimal(uc.getQuantity()));
                                        ar2.setSign("1");
                                        ar2.setOrderNo(o.getOrderNo());
                                        ar2.setInfo(customer.getAgentCode() + "参团成功! 未抽中,拼团券退回：" + quantity1);
                                        tc.accountRecordDao.save(ar2);
                                    }
                                }

                                // acc.setVoucherBalance(acc.getVoucherBalance().add(product.getVoucher()));
                                // tc.accountDao.update(acc);

//                                AccountRecord ar1_2 = new AccountRecord();
//                                ar1_2.setIntoUserId(o.getOrderPersonId());
//                                ar1_2.setUpdateTime(new Date());
//                                ar1_2.setType("0");
//                                ar1_2.setAmount(product.getVoucher());
//                                ar1_2.setNowAmount(acc.getVoucherBalance());
//                                ar1_2.setSign("1");
//                                ar1_2.setOrderNo(o.getOrderNo());
//                                ar1_2.setInfo(customer.getAgentCode() + "参团成功! 未抽中,送您余额：" + product.getVoucher());
//                                tc.accountRecordDao.save(ar1_2);

                                FailRedPack frp = new FailRedPack();
                                frp.setOrderNo(o.getOrderNo());
                                frp.setPrizeMoney(product.getVoucher());
                                frp.setStatus("2"); // 未开红包
                                frp.setUpdateTime(new Date());
                                frp.setUserId(o.getOrderPersonId());
                                frp.setMode("0");
                                tc.failRedPackDao.save(frp);

                                // 处理参与拼团后的利润分配
                                // tc.ordersCopyService.handleCollageProfit(o.getOrderPersonId(), product.getVoucher(), o.getOrderNo());

                            }
                        }
                    }
                }
                mp.setIsWaitFor("0"); // 不用再等待了
                tc.modeProductDao.update(mp);
                System.err.println("处理极乐桑巴团 -------------------------当前团已修改不需要再等待------------------------------------");
            }
        }
        System.err.println("每天 上午十点整，下午三点整，晚上八点，处理极乐桑巴团 正式 -------------------------------结束------------------------------");
    }


    // 每天 上午十点三十五，下午三点三十五，晚上八点三十五，处理极乐桑巴团 虚拟
    //@Scheduled(cron = "0 35 15 * * ?") // 线上为 10:35,15:35,20:35  "0 35 10,15,20 * * ?"   现调整为每天 15:35
    public void TimingBusiness11(){
        System.err.println("每天 上午十点三十五，下午三点三十五，晚上八点三十五，处理极乐桑巴团 虚拟 ----------------开始------------虚拟---------------------------------");
        // 查询极乐桑巴正在进行中的团
        List<ModeProduct> mpList2 = tc.modeProductDao.queryByOnGoing();
        if(mpList2.size() > 0){
            System.err.println("查询极乐桑巴正在进行中的团 ----------------------------未成功,需要添加虚拟数据去成功的---------------------------------" + mpList2.size());
            for(int y = 0; y < mpList2.size(); y++){
                ModeProduct mp = mpList2.get(y);
                CollageMode cm = tc.collageModeDao.queryById(mp.getModeId());
                Integer quantity = cm.getPeopleQuantity();

                List<CollageUser> cuList = tc.collageUserDao.queryByModeProductId(mp.getId());
                Integer rowSize = cuList.size();
                if(rowSize == 0){
                    // 不作处理
                }else{
                    if (rowSize < quantity) {
                        System.err.println("添加虚拟数据++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                        // 把当前团直接成功
                        mp.setStatus("1");
                        tc.modeProductDao.update(mp);
                        Integer cha = quantity - rowSize;
                        // 准备加入虚拟数据，让团成立
                        List<Customer> cList = tc.customerDao.queryByFiction();
                        for (int i = 0; i < cha; i++) {
                            int userId = 100 + (int) (Math.random() * (cList.size() + 1 - 1));
                            Customer customer = tc.customerDao.queryById(userId);
                            if (customer == null) {
                                customer = tc.customerDao.queryById(666);
                            }
                            CollageUser newCU = new CollageUser();
                            newCU.setModeProductId(mp.getId());
                            newCU.setUserId(customer.getId());
                            newCU.setUpdateTime(new Date());
                            newCU.setFiction("1"); // 虚拟数据
                            tc.collageUserDao.save(newCU);
                        }
                    }

                    List<CollageUser> cuList1 = tc.collageUserDao.queryByModeProductId(mp.getId());
                    Integer rowSize1 = cuList1.size();
                    if (rowSize1 == quantity) {
                        System.err.println("开始处理中奖和退款++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                        // 随机一个人中奖，其他的人判断是否要退款，虚拟数据不做任何处理
                        // 获取真实的数据，抽一条中奖
                        List<CollageUser> cuList1_1 = tc.collageUserDao.queryByModeProductId_1(mp.getId());
                        Integer rowSize1_1 = cuList1_1.size();
                        Random random = new Random();
                        int n = random.nextInt(rowSize1_1); // 随机抽中一条数据
                        for(int u = 0; u < rowSize1_1; u++){
                            CollageUser cu = cuList1_1.get(u);
                            if(u == n){
                                if("1".equals(cu.getFiction())){
                                    // 虚拟用户中奖了

                                }else{
                                    // 正式用户中奖了
                                    Orders orders = tc.ordersDao.queryByIsO(cu.getUserId(), mp.getId());
                                    if(orders != null){
                                        Product product = tc.productDao.queryById(orders.getProductId());
                                        Customer customer = tc.customerDao.queryById(orders.getOrderPersonId());
                                        // 添加销量
                                        product.setSales(product.getSales() + 1);
                                        tc.productDao.update(product);

                                        // 中奖
                                        // 中奖
                                        if (orders.getCollageModeId() == 2) { // 十八人团
                                            orders.setDealStatus("1"); // 待发货
                                        } else if (orders.getCollageModeId() == 3) { // 九人团
                                            orders.setDealStatus("7"); // 代售
                                        } else if (orders.getCollageModeId() == 4) { // 溜溜溜
                                            orders.setDealStatus("1"); // 待发货
                                        } else if (orders.getCollageModeId() == 20) { // 速度与激情
                                            orders.setDealStatus("1"); // 待发货
                                        }
                                        tc.ordersDao.update(orders);

                                        Account acc = tc.accountDao.queryAccountByUserId(orders.getOrderPersonId());
                                        // 送3个极乐值
                                        acc.setBliss(acc.getBliss() + 3);
                                        // 送等额的积分
                                        int give = (int)Math.round(orders.getPayAmount().doubleValue());
                                        acc.setIntegral(acc.getIntegral() + give);
                                        // 拼中也送红包余额
                                        acc.setVoucherBalance(acc.getVoucherBalance().add(product.getVoucher1()));
                                        tc.accountDao.update(acc);

                                        AccountRecord ar3 = new AccountRecord();
                                        ar3.setIntoUserId(orders.getOrderPersonId());
                                        ar3.setOrderNo(orders.getOrderNo());
                                        ar3.setType("2");
                                        ar3.setSign("1");
                                        ar3.setUpdateTime(new Date());
                                        ar3.setAmount(new BigDecimal(3));
                                        ar3.setNowAmount(new BigDecimal(acc.getBliss()));
                                        ar3.setInfo(customer.getAgentCode() + "参团成功! 拼中产品获得极乐值：" + 3);
                                        tc.accountRecordDao.save(ar3);

                                        AccountRecord ar3_1 = new AccountRecord();
                                        ar3_1.setIntoUserId(orders.getOrderPersonId());
                                        ar3_1.setOrderNo(orders.getOrderNo());
                                        ar3_1.setType("1");
                                        ar3_1.setSign("1");
                                        ar3_1.setUpdateTime(new Date());
                                        ar3_1.setAmount(new BigDecimal(give));
                                        ar3_1.setNowAmount(new BigDecimal(acc.getIntegral()));
                                        ar3_1.setInfo(customer.getAgentCode() + "参团成功! 拼中产品获得积分：" + give);
                                        tc.accountRecordDao.save(ar3_1);

                                        AccountRecord ar3_2 = new AccountRecord();
                                        ar3_2.setIntoUserId(orders.getOrderPersonId());
                                        ar3_2.setUpdateTime(new Date());
                                        ar3_2.setType("0");
                                        ar3_2.setAmount(product.getVoucher1());
                                        ar3_2.setNowAmount(acc.getVoucherBalance());
                                        ar3_2.setSign("1");
                                        ar3_2.setOrderNo(orders.getOrderNo());
                                        ar3_2.setInfo(customer.getAgentCode() + "参团成功! 拼中产品获得红包余额：" + product.getVoucher1());
                                        tc.accountRecordDao.save(ar3_2);

                                        // 处理参与拼团后的利润分配
                                        tc.ordersCopyService.handleCollageProfit(orders.getOrderPersonId(), product.getVoucher1(), orders.getOrderNo());

                                        // 极乐桑巴
                                        if (orders.getCollageModeId() == 2) {
                                            // 判断当前产品是否满六次，执行全额退款
                                            CollageInRecord2 cir2 = tc.collageInRecord2Dao.queryCir2ByIds(orders.getCollageModeId(), orders.getProductId(), orders.getOrderPersonId());
                                            if (cir2 == null) {
                                                // 第一次中当前产品
                                                CollageInRecord2 newCir2 = new CollageInRecord2();
                                                newCir2.setModeId(orders.getCollageModeId());
                                                newCir2.setProductId(orders.getProductId());
                                                newCir2.setUserId(orders.getOrderPersonId());
                                                newCir2.setStatus("0");
                                                newCir2.setTotalQuantity(1);
                                                newCir2.setPayAmount(orders.getPayAmount());
                                                newCir2.setUpdateTime(new Date());
                                                tc.collageInRecord2Dao.save(newCir2);
                                            } else {
                                                // TODO 将6改成3
                                                if (cir2.getTotalQuantity() < 3) {
                                                    // 继续累积
                                                    cir2.setPayAmount(cir2.getPayAmount().add(orders.getPayAmount()));
                                                    cir2.setTotalQuantity(cir2.getTotalQuantity() + 1);
                                                    cir2.setUpdateTime(new Date());
                                                    tc.collageInRecord2Dao.update(cir2);
                                                } else {
                                                    // 满足六次，退钱六次的钱
                                                    cir2.setStatus("1");
                                                    cir2.setUpdateTime(new Date());
                                                    tc.collageInRecord2Dao.update(cir2);

                                                    Account accCir = tc.accountDao.queryAccountByUserId(orders.getOrderPersonId());
                                                    accCir.setVoucherBalance(accCir.getVoucherBalance().add(cir2.getPayAmount()));
                                                    tc.accountDao.update(accCir);

                                                    AccountRecord arCir = new AccountRecord();
                                                    arCir.setIntoUserId(orders.getOrderPersonId());
                                                    arCir.setUpdateTime(new Date());
                                                    arCir.setType("0");
                                                    arCir.setAmount(cir2.getPayAmount());
                                                    arCir.setNowAmount(accCir.getVoucherBalance());
                                                    arCir.setSign("1");
                                                    arCir.setOrderNo(orders.getOrderNo());
                                                    // TODO 将6改成3
                                                    arCir.setInfo(customer.getAgentCode() + "参团成功! 累积拼中产品三次,获得广告补贴为：" + cir2.getPayAmount());
                                                    tc.accountRecordDao.save(arCir);

                                                    // 送共享券
                                                    UserCoupon userCoupon = tc.userCouponDao.queryByUserIdAndCouponId(cir2.getUserId(), 2);
                                                    if (userCoupon != null) {
                                                        userCoupon.setQuantity(userCoupon.getQuantity() + 1);
                                                        userCoupon.setUpdateTime(new Date());
                                                        tc.userCouponDao.update(userCoupon);

                                                        AccountRecord ar1 = new AccountRecord();
                                                        ar1.setIntoUserId(cir2.getUserId());
                                                        ar1.setUpdateTime(new Date());
                                                        ar1.setType("6");
                                                        ar1.setAmount(new BigDecimal(1));
                                                        ar1.setNowAmount(new BigDecimal(userCoupon.getQuantity()));
                                                        ar1.setSign("1");
                                                        // TODO 将6改成3
                                                        ar1.setInfo(tc.customerDao.queryById(cir2.getUserId()).getAgentCode() + "参团成功! 累积拼中产品三次,退款总金额并赠送共享券：" + 1);
                                                        tc.accountRecordDao.save(ar1);
                                                    } else {
                                                        UserCoupon newUc = new UserCoupon();
                                                        newUc.setQuantity(1);
                                                        newUc.setUpdateTime(new Date());
                                                        newUc.setCouponId(2);
                                                        newUc.setUserId(cir2.getUserId());
                                                        tc.userCouponDao.save(newUc);

                                                        AccountRecord ar2 = new AccountRecord();
                                                        ar2.setIntoUserId(cir2.getUserId());
                                                        ar2.setUpdateTime(new Date());
                                                        ar2.setType("6");
                                                        ar2.setAmount(new BigDecimal(1));
                                                        ar2.setNowAmount(new BigDecimal(newUc.getQuantity()));
                                                        ar2.setSign("1");
                                                        // TODO 将6改成3
                                                        ar2.setInfo(tc.customerDao.queryById(cir2.getUserId()).getAgentCode() + "参团成功! 累积拼中产品三次,退款总金额并赠送共享券：" + 1);
                                                        tc.accountRecordDao.save(ar2);
                                                    }

                                                    // 存下第七次
                                                    CollageInRecord2 newCir7 = new CollageInRecord2();
                                                    newCir7.setModeId(orders.getCollageModeId());
                                                    newCir7.setProductId(orders.getProductId());
                                                    newCir7.setUserId(orders.getOrderPersonId());
                                                    newCir7.setStatus("0");
                                                    newCir7.setTotalQuantity(1);
                                                    newCir7.setPayAmount(orders.getPayAmount());
                                                    newCir7.setUpdateTime(new Date());
                                                    tc.collageInRecord2Dao.save(newCir7);
                                                }
                                            }

                                            // 判断当前拼团模式中是否存在满足六次的，有就执行退款
                                            // TODO 将6改成3
                                            CollageInRecord2 cir2_1 = tc.collageInRecord2Dao.queryCir2ByIds_1(orders.getCollageModeId(), orders.getProductId(), orders.getOrderPersonId());
                                            if (cir2_1 != null) {
                                                // 满足六次，退钱六次的钱
                                                cir2_1.setStatus("1");
                                                cir2_1.setUpdateTime(new Date());
                                                tc.collageInRecord2Dao.update(cir2_1);

                                                Account accCir_1 = tc.accountDao.queryAccountByUserId(orders.getOrderPersonId());
                                                accCir_1.setVoucherBalance(accCir_1.getVoucherBalance().add(cir2_1.getPayAmount()));
                                                tc.accountDao.update(accCir_1);

                                                AccountRecord arCir_1 = new AccountRecord();
                                                arCir_1.setIntoUserId(orders.getOrderPersonId());
                                                arCir_1.setUpdateTime(new Date());
                                                arCir_1.setType("0");
                                                arCir_1.setAmount(cir2_1.getPayAmount());
                                                arCir_1.setNowAmount(accCir_1.getVoucherBalance());
                                                arCir_1.setSign("1");
                                                arCir_1.setOrderNo(orders.getOrderNo());
                                                // TODO 将6改成3
                                                arCir_1.setInfo(customer.getAgentCode() + "参团成功! 累积拼中产品三次,获得广告补贴为：" + cir2_1.getPayAmount());
                                                tc.accountRecordDao.save(arCir_1);

                                                // 送共享券
                                                UserCoupon userCoupon = tc.userCouponDao.queryByUserIdAndCouponId(cir2_1.getUserId(), 2);
                                                if (userCoupon != null) {
                                                    userCoupon.setQuantity(userCoupon.getQuantity() + 1);
                                                    userCoupon.setUpdateTime(new Date());
                                                    tc.userCouponDao.update(userCoupon);

                                                    AccountRecord ar1 = new AccountRecord();
                                                    ar1.setIntoUserId(cir2_1.getUserId());
                                                    ar1.setUpdateTime(new Date());
                                                    ar1.setType("6");
                                                    ar1.setAmount(new BigDecimal(1));
                                                    ar1.setNowAmount(new BigDecimal(userCoupon.getQuantity()));
                                                    ar1.setSign("1");
                                                    // TODO 将6改成3
                                                    ar1.setInfo(tc.customerDao.queryById(cir2_1.getUserId()).getAgentCode() + "参团成功! 累积拼中产品三次,退款总金额并赠送共享券：" + 1);
                                                    tc.accountRecordDao.save(ar1);
                                                } else {
                                                    UserCoupon newUc = new UserCoupon();
                                                    newUc.setQuantity(1);
                                                    newUc.setUpdateTime(new Date());
                                                    newUc.setCouponId(2);
                                                    newUc.setUserId(cir2_1.getUserId());
                                                    tc.userCouponDao.save(newUc);

                                                    AccountRecord ar2 = new AccountRecord();
                                                    ar2.setIntoUserId(cir2_1.getUserId());
                                                    ar2.setUpdateTime(new Date());
                                                    ar2.setType("6");
                                                    ar2.setAmount(new BigDecimal(1));
                                                    ar2.setNowAmount(new BigDecimal(newUc.getQuantity()));
                                                    ar2.setSign("1");
                                                    // TODO 将6改成3
                                                    ar2.setInfo(tc.customerDao.queryById(cir2_1.getUserId()).getAgentCode() + "参团成功! 累积拼中产品三次,退款总金额并赠送共享券：" + 1);
                                                    tc.accountRecordDao.save(ar2);
                                                }
                                            }

                                        }

                                    }
                                }
                            }else{
                                if("1".equals(cu.getFiction())){
                                    // 虚拟用户退款

                                }else{
                                    // 正式用户退款，判断是否勾选拼中为止
                                    Orders orders = tc.ordersDao.queryByIsO(cu.getUserId(), mp.getId());
                                    if(orders != null){
                                        Product product = tc.productDao.queryById(orders.getProductId());
                                        Customer customer = tc.customerDao.queryById(orders.getOrderPersonId());
                                        BigDecimal couponAmount = null;
                                        if(orders.getCouponAmount() == null){
                                            couponAmount = new BigDecimal(0);
                                        }else{
                                            couponAmount = orders.getCouponAmount();
                                        }
                                        if("1".equals(orders.getLastCollage())){

                                            // TODO 当前订单转移到 OrdersCopy
                                            OrdersCopy oc = new OrdersCopy();
                                            oc.setId(orders.getId());
                                            oc.setOrderNo(orders.getOrderNo());
                                            oc.setTradeNo(orders.getTradeNo());
                                            oc.setTotalAmount(orders.getTotalAmount());
                                            oc.setFreight(orders.getFreight());
                                            oc.setCollageModeId(orders.getCollageModeId());
                                            oc.setOrderPersonId(orders.getOrderPersonId());
                                            oc.setLastCollage(orders.getLastCollage());
                                            oc.setConsigneeName(orders.getConsigneeName());
                                            oc.setConsigneePhone(orders.getConsigneePhone());
                                            oc.setAddress(orders.getAddress());
                                            oc.setUpdateTime(new Date());
                                            oc.setPayAmount(orders.getPayAmount());
                                            oc.setCouponAmount(orders.getCouponAmount());
                                            oc.setModeProductId(orders.getModeProductId());
                                            oc.setProductId(orders.getProductId());
                                            oc.setCouponId(1);
                                            oc.setSkuId(tc.orderDetailsDao.queryByOrderNo(orders.getOrderNo()).getRelationId());
                                            if(couponAmount.doubleValue() > 0){
                                                int quantity1 = (int) (couponAmount.doubleValue() / 9.9);
                                                oc.setCouponQuantity(quantity1);
                                            }else{
                                                oc.setCouponQuantity(0);
                                            }
                                            tc.ordersCopyDao.save(oc);

                                            // 给个红包
//                                            Account accN = tc.accountDao.queryAccountByUserId(orders.getOrderPersonId());
//                                            accN.setVoucherBalance(accN.getVoucherBalance().add(product.getVoucher()));
//                                            tc.accountDao.update(accN);
//
//                                            AccountRecord ar1_2 = new AccountRecord();
//                                            ar1_2.setIntoUserId(orders.getOrderPersonId());
//                                            ar1_2.setUpdateTime(new Date());
//                                            ar1_2.setType("0");
//                                            ar1_2.setAmount(product.getVoucher());
//                                            ar1_2.setNowAmount(accN.getVoucherBalance());
//                                            ar1_2.setSign("1");
//                                            ar1_2.setOrderNo(orders.getOrderNo());
//                                            ar1_2.setInfo(customer.getAgentCode() + "参团成功! 未抽中,送您余额：" + product.getVoucher());
//                                            tc.accountRecordDao.save(ar1_2);

                                            FailRedPack frp = new FailRedPack();
                                            frp.setOrderNo(orders.getOrderNo());
                                            frp.setPrizeMoney(product.getVoucher());
                                            frp.setStatus("2"); // 未开红包
                                            frp.setUpdateTime(new Date());
                                            frp.setUserId(orders.getOrderPersonId());
                                            frp.setMode("0");
                                            tc.failRedPackDao.save(frp);

                                            // 处理参与拼团后的利润分配
                                            // tc.ordersCopyService.handleCollageProfit(orders.getOrderPersonId(), product.getVoucher(), orders.getOrderNo());

                                        }else{
                                            // 执行退款(退到余额)退券 把每个人领取到的红包进行分润
                                            orders.setDealStatus("4"); // 交易关闭
                                            orders.setRefundStatus("2"); // 退款成功
                                            orders.setPayStatus("3"); // 已退款
                                            tc.ordersDao.update(orders);

                                            Account acc = tc.accountDao.queryAccountByUserId(orders.getOrderPersonId());
                                            acc.setVoucherBalance(acc.getVoucherBalance().add(orders.getPayAmount()));
                                            tc.accountDao.update(acc);

                                            AccountRecord ar1_1 = new AccountRecord();
                                            ar1_1.setIntoUserId(orders.getOrderPersonId());
                                            ar1_1.setUpdateTime(new Date());
                                            ar1_1.setType("0");
                                            ar1_1.setAmount(orders.getPayAmount());
                                            ar1_1.setNowAmount(acc.getVoucherBalance());
                                            ar1_1.setSign("1");
                                            ar1_1.setOrderNo(orders.getOrderNo());
                                            ar1_1.setInfo(customer.getAgentCode() + "参团成功! 未抽中,支付金额退回至余额：" + orders.getPayAmount());
                                            tc.accountRecordDao.save(ar1_1);

                                            if(couponAmount.doubleValue() > 0){
                                                int quantity1 = (int) (couponAmount.doubleValue() / 9.9);
                                                if(quantity1 > 0){
                                                    UserCoupon uc = tc.userCouponDao.queryByUserIdAndCouponId(orders.getOrderPersonId(), 1);
                                                    if(uc != null){
                                                        uc.setQuantity(uc.getQuantity() + quantity1);
                                                        tc.userCouponDao.update(uc);

                                                        AccountRecord ar2 = new AccountRecord();
                                                        ar2.setIntoUserId(orders.getOrderPersonId());
                                                        ar2.setUpdateTime(new Date());
                                                        ar2.setType("5"); // 拼团券
                                                        ar2.setAmount(new BigDecimal(quantity1));
                                                        ar2.setNowAmount(new BigDecimal(uc.getQuantity()));
                                                        ar2.setSign("1");
                                                        ar2.setOrderNo(orders.getOrderNo());
                                                        ar2.setInfo(customer.getAgentCode() + "参团成功! 未抽中,拼团券退回：" + quantity1);
                                                        tc.accountRecordDao.save(ar2);
                                                    }
                                                }
                                            }

                                            // acc.setVoucherBalance(acc.getVoucherBalance().add(product.getVoucher()));
                                            // tc.accountDao.update(acc);

//                                            AccountRecord ar1_2 = new AccountRecord();
//                                            ar1_2.setIntoUserId(orders.getOrderPersonId());
//                                            ar1_2.setUpdateTime(new Date());
//                                            ar1_2.setType("0");
//                                            ar1_2.setAmount(product.getVoucher());
//                                            ar1_2.setNowAmount(acc.getVoucherBalance());
//                                            ar1_2.setSign("1");
//                                            ar1_2.setOrderNo(orders.getOrderNo());
//                                            ar1_2.setInfo(customer.getAgentCode() + "参团成功! 未抽中,送您余额：" + product.getVoucher());
//                                            tc.accountRecordDao.save(ar1_2);

                                            FailRedPack frp = new FailRedPack();
                                            frp.setOrderNo(orders.getOrderNo());
                                            frp.setPrizeMoney(product.getVoucher());
                                            frp.setStatus("2"); // 未开红包
                                            frp.setUpdateTime(new Date());
                                            frp.setUserId(orders.getOrderPersonId());
                                            frp.setMode("0");
                                            tc.failRedPackDao.save(frp);

                                            // 处理参与拼团后的利润分配
                                            // tc.ordersCopyService.handleCollageProfit(orders.getOrderPersonId(), product.getVoucher(), orders.getOrderNo());

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.err.println("每天 上午十点三十五，下午三点三十五，晚上八点三十五，处理极乐桑巴团 虚拟 -------------------------------结束------------------------------");
    }


    // 每隔5分钟执行一次 查看团是否需要自动完成 处理九人团和六人团以及速度与激情
    //@Scheduled(cron = "0 */5 * * * ?")
    public void TimingBusiness12() throws ParseException {
        System.err.println("每隔五分钟执行一次 查看九人团和六人团及速度与激情是否需要自动完成++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = new Date(); // 系统当前时间
        String newTime = df.format(nowDate);
        String time02 = newTime + " 02:00:00";
        Date date02 = sdf.parse(time02);
        String time05 = newTime + " 05:00:00";
        Date date05 = sdf.parse(time05);
        // 每天凌晨 02:00 - 05:00 不执行
        if(nowDate.after(date02) && nowDate.before(date05)) {
            System.err.println("每天凌晨两点至五点不自动成团!!!++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        }else{
            List<ModeProduct> mpList = tc.modeProductDao.queryByStatus(); // 速度与激情暂时不自动成团 where status = 0 and (mode_id = 3 OR mode_id = 4 OR mode_id = 20)
            if (mpList.size() > 0) {
                System.err.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++ 进行中的团：" + mpList.size());
                for (ModeProduct mp : mpList) {
                    Date date = mp.getUpdateTime();
                    // date 时间 加 半个小时
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    calendar.add(Calendar.MINUTE, 30); // 线上加30分钟
                    Date date1 = calendar.getTime();
                    Date date2 = new Date();
                    // 加30分钟时间后再系统时间之后
                    if (date2.after(date1)) {
                        System.err.println("有团 需要自动完成++++++++++++++++++++++++++++++++++++++++++++++++++++++ 团ID：" + mp.getId());
                        CollageMode cm = tc.collageModeDao.queryById(mp.getModeId());
                        Integer quantity = cm.getPeopleQuantity();

                        List<CollageUser> cuList = tc.collageUserDao.queryByModeProductId(mp.getId());
                        Integer rowSize = cuList.size();
                        if(rowSize == 0){
                            System.err.println("当前团一个人都没有，暂时不做任何处理++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                        }else{
                            if (rowSize < quantity) {
                                System.err.println("添加虚拟数据++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                                // 把当前团直接成功
                                mp.setStatus("1");
                                tc.modeProductDao.update(mp);
                                Integer cha = quantity - rowSize;
                                // 准备加入虚拟数据，让团成立
                                List<Customer> cList = tc.customerDao.queryByFiction();
                                for (int i = 0; i < cha; i++) {
                                    int userId = 100 + (int) (Math.random() * (cList.size() + 1 - 1));
                                    Customer customer = tc.customerDao.queryById(userId);
                                    if (customer == null) {
                                        customer = tc.customerDao.queryById(666);
                                    }
                                    CollageUser newCU = new CollageUser();
                                    newCU.setModeProductId(mp.getId());
                                    newCU.setUserId(customer.getId());
                                    newCU.setUpdateTime(new Date());
                                    newCU.setFiction("1"); // 虚拟数据
                                    tc.collageUserDao.save(newCU);
                                }
                            }

                            List<CollageUser> cuList1 = tc.collageUserDao.queryByModeProductId(mp.getId());
                            Integer rowSize1 = cuList1.size();
                            if (rowSize1 == quantity) {
                                System.err.println("开始处理中奖和退款++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                                // 随机一个人中奖，其他的人判断是否要退款，虚拟数据不做任何处理
                                // 获取真实的数据，抽一条中奖
                                List<CollageUser> cuList1_1 = tc.collageUserDao.queryByModeProductId_1(mp.getId());
                                Integer rowSize1_1 = cuList1_1.size();
                                Random random = new Random();
                                int n = random.nextInt(rowSize1_1); // 随机抽中一条数据
                                for(int u = 0; u < rowSize1_1; u++){
                                    CollageUser cu = cuList1_1.get(u);
                                    if(u == n){
                                        if("1".equals(cu.getFiction())){
                                            // 虚拟用户中奖了

                                        }else{
                                            // 正式用户中奖了
                                            Orders orders = tc.ordersDao.queryByIsO(cu.getUserId(), mp.getId());
                                            if(orders != null){
                                                Product product = tc.productDao.queryById(orders.getProductId());
                                                Customer customer = tc.customerDao.queryById(orders.getOrderPersonId());
                                                // 添加销量
                                                product.setSales(product.getSales() + 1);
                                                tc.productDao.update(product);

                                                // 中奖
                                                if(orders.getCollageModeId() == 3){
                                                    orders.setDealStatus("7"); // 代售
                                                }else if(orders.getCollageModeId() == 4){
                                                    orders.setDealStatus("1"); // 待发货
                                                }else if(orders.getCollageModeId() == 20){
                                                    orders.setDealStatus("1"); // 待发货
                                                }
                                                tc.ordersDao.update(orders);

                                                Account acc = tc.accountDao.queryAccountByUserId(orders.getOrderPersonId());
                                                // 送3个极乐值
                                                acc.setBliss(acc.getBliss() + 3);
                                                // 送等额的积分
                                                int give = (int)Math.round(orders.getPayAmount().doubleValue());
                                                acc.setIntegral(acc.getIntegral() + give);
                                                // 拼中也送红包余额
                                                acc.setVoucherBalance(acc.getVoucherBalance().add(product.getVoucher1()));
                                                tc.accountDao.update(acc);

                                                AccountRecord ar3 = new AccountRecord();
                                                ar3.setIntoUserId(orders.getOrderPersonId());
                                                ar3.setOrderNo(orders.getOrderNo());
                                                ar3.setType("2");
                                                ar3.setSign("1");
                                                ar3.setUpdateTime(new Date());
                                                ar3.setAmount(new BigDecimal(3));
                                                ar3.setNowAmount(new BigDecimal(acc.getBliss()));
                                                ar3.setInfo(customer.getAgentCode() + "参团成功! 拼中产品获得极乐值：" + 3);
                                                tc.accountRecordDao.save(ar3);

                                                AccountRecord ar3_1 = new AccountRecord();
                                                ar3_1.setIntoUserId(orders.getOrderPersonId());
                                                ar3_1.setOrderNo(orders.getOrderNo());
                                                ar3_1.setType("1");
                                                ar3_1.setSign("1");
                                                ar3_1.setUpdateTime(new Date());
                                                ar3_1.setAmount(new BigDecimal(give));
                                                ar3_1.setNowAmount(new BigDecimal(acc.getIntegral()));
                                                ar3_1.setInfo(customer.getAgentCode() + "参团成功! 拼中产品获得积分：" + give);
                                                tc.accountRecordDao.save(ar3_1);

                                                AccountRecord ar3_2 = new AccountRecord();
                                                ar3_2.setIntoUserId(orders.getOrderPersonId());
                                                ar3_2.setUpdateTime(new Date());
                                                ar3_2.setType("0");
                                                ar3_2.setAmount(product.getVoucher1());
                                                ar3_2.setNowAmount(acc.getVoucherBalance());
                                                ar3_2.setSign("1");
                                                ar3_2.setOrderNo(orders.getOrderNo());
                                                ar3_2.setInfo(customer.getAgentCode() + "参团成功! 拼中产品获得红包余额：" + product.getVoucher1());
                                                tc.accountRecordDao.save(ar3_2);

                                                // 处理参与拼团后的利润分配
                                                tc.ordersCopyService.handleCollageProfit(orders.getOrderPersonId(), product.getVoucher1(), orders.getOrderNo());

                                                // 喜欢就拼
                                                if(orders.getCollageModeId() == 3){
                                                    // 判断当前产品是否满十二次，执行每月代售
                                                    // 查看当前模式当前产品当前人 一共中了多少条
                                                    List<CollageInRecord3> cir3List = tc.collageInRecord3Dao.queryCir3ByIds(orders.getCollageModeId(), orders.getProductId(), orders.getOrderPersonId());
                                                    List<CollageInRecord3> cir3List_copy = cir3List;
                                                    int cir3Size = cir3List.size();
                                                    if(cir3Size > 0){
                                                        // TODO 将12改成3
                                                        if(cir3Size < 3){
                                                            // 未满十二次直接加一条
                                                            CollageInRecord3 newCir3 = new CollageInRecord3();
                                                            newCir3.setModeId(orders.getCollageModeId());
                                                            newCir3.setProductId(orders.getProductId());
                                                            newCir3.setUserId(orders.getOrderPersonId());
                                                            newCir3.setStatus("0"); // 未返还
                                                            newCir3.setIsTake("1"); // 还能提货
                                                            newCir3.setOrderNo(orders.getOrderNo());
                                                            newCir3.setPayAmount(orders.getPayAmount());
                                                            newCir3.setUpdateTime(orders.getUpdateTime());
                                                            tc.collageInRecord3Dao.save(newCir3);

                                                            // 查看CollageInRecord3Copy当前模式当前产品当前人一共累计有多少
                                                            CollageInRecord3Copy cir3Copy = tc.collageInRecord3CopyDao.queryByIds(orders.getCollageModeId(), orders.getProductId(), orders.getOrderPersonId());
                                                            if(cir3Copy == null){
                                                                // 创建并累计一次
                                                                CollageInRecord3Copy newCir3Copy = new CollageInRecord3Copy();
                                                                newCir3Copy.setModeId(orders.getCollageModeId());
                                                                newCir3Copy.setProductId(orders.getProductId());
                                                                newCir3Copy.setUserId(orders.getOrderPersonId());
                                                                newCir3Copy.setTotalQuantity(1);
                                                                newCir3Copy.setStatus("0");
                                                                newCir3Copy.setPayAmount(orders.getPayAmount());
                                                                newCir3Copy.setUpdateTime(new Date());
                                                                tc.collageInRecord3CopyDao.save(newCir3Copy);
                                                            }else{
                                                                // 直接累计一次
                                                                cir3Copy.setTotalQuantity(cir3Copy.getTotalQuantity() + 1);
                                                                cir3Copy.setPayAmount(cir3Copy.getPayAmount().add(orders.getPayAmount()));
                                                                cir3Copy.setUpdateTime(new Date());
                                                                tc.collageInRecord3CopyDao.update(cir3Copy);
                                                            }

                                                        }else{
                                                            // 满足十二次了
                                                            // 查看当前模式当前产品当前人下面有多少个代售 还没有返还能提货的条数
                                                            List<CollageInRecord3> getCir3List = tc.collageInRecord3Dao.queryCir3ByIds_1(orders.getCollageModeId(), orders.getProductId(), orders.getOrderPersonId());
                                                            if(getCir3List.size() > 0){
                                                                CollageInRecord3 getCir3 = getCir3List.get(0);
                                                                CollageInRecord3 getCir3_1 = null;
                                                                if(getCir3List.size() > 1){
                                                                    getCir3_1 = getCir3List.get(1);
                                                                }
                                                                String time = df.format(orders.getUpdateTime());
                                                                boolean temp = false;
                                                                for(int v = 0; v < cir3Size; v++){
                                                                    CollageInRecord3 cir3 = cir3List.get(v);
                                                                    if(getCir3.getId() == cir3.getId()){ // 未返并能提货中的第一个去匹配所有的得到一条数据进行代售

                                                                        cir3.setStatus("1"); // 已返还
                                                                        cir3.setEstimateReturn(time); // 当天返回一个代售   yyyy-MM-dd  时间为当下返回的代售标记

                                                                        // 退钱
                                                                        Account acc3 = tc.accountDao.queryAccountByUserId(orders.getOrderPersonId());
                                                                        acc3.setVoucherBalance(acc3.getVoucherBalance().add(cir3.getPayAmount()));
                                                                        tc.accountDao.update(acc3);

                                                                        AccountRecord ar = new AccountRecord();
                                                                        ar.setIntoUserId(orders.getOrderPersonId());
                                                                        ar.setUpdateTime(new Date());
                                                                        ar.setType("0");
                                                                        ar.setAmount(cir3.getPayAmount());
                                                                        ar.setNowAmount(acc3.getVoucherBalance());
                                                                        ar.setSign("1");
                                                                        ar.setOrderNo(orders.getOrderNo());
                                                                        // TODO 将12改成3
                                                                        ar.setInfo(customer.getAgentCode() + "参团成功! 累计抽中已满三次, 当下返回一个代售：" + cir3.getPayAmount());
                                                                        tc.accountRecordDao.save(ar);

                                                                        // 修改订单信息
                                                                        Orders orders1 = tc.ordersDao.queryByOrderNo(cir3.getOrderNo());
                                                                        if(orders1 != null){
                                                                            orders1.setDealStatus("4"); // 交易关闭
                                                                            orders1.setRefundStatus("2"); // 退款成功
                                                                            orders1.setPayStatus("3"); // 已退款
                                                                            tc.ordersDao.update(orders1);
                                                                        }

                                                                        // 送共享券
                                                                        UserCoupon userCoupon = tc.userCouponDao.queryByUserIdAndCouponId(cir3.getUserId(), 2); // 2 是共享券
                                                                        if(userCoupon != null){
                                                                            userCoupon.setQuantity(userCoupon.getQuantity() + 1);
                                                                            userCoupon.setUpdateTime(new Date());
                                                                            tc.userCouponDao.update(userCoupon);

                                                                            AccountRecord ar1 = new AccountRecord();
                                                                            ar1.setIntoUserId(cir3.getUserId());
                                                                            ar1.setUpdateTime(new Date());
                                                                            ar1.setType("6");
                                                                            ar1.setAmount(new BigDecimal(1));
                                                                            ar1.setNowAmount(new BigDecimal(userCoupon.getQuantity()));
                                                                            ar1.setSign("1");
                                                                            // TODO 将12改成3
                                                                            ar1.setInfo(tc.customerDao.queryById(cir3.getUserId()).getAgentCode() + "参团成功! 累计抽中已满三次, 当下返还代售时赠送共享券：" + 1);
                                                                            tc.accountRecordDao.save(ar1);
                                                                        }else{
                                                                            UserCoupon newUc = new UserCoupon();
                                                                            newUc.setQuantity(1);
                                                                            newUc.setUpdateTime(new Date());
                                                                            newUc.setCouponId(2);
                                                                            newUc.setUserId(cir3.getUserId());
                                                                            tc.userCouponDao.save(newUc);

                                                                            AccountRecord ar2 = new AccountRecord();
                                                                            ar2.setIntoUserId(cir3.getUserId());
                                                                            ar2.setUpdateTime(new Date());
                                                                            ar2.setType("6");
                                                                            ar2.setAmount(new BigDecimal(1));
                                                                            ar2.setNowAmount(new BigDecimal(newUc.getQuantity()));
                                                                            ar2.setSign("1");
                                                                            // TODO 将12改成3
                                                                            ar2.setInfo(tc.customerDao.queryById(cir3.getUserId()).getAgentCode() + "参团成功! 累计抽中已满三次, 当下返还代售时赠送共享券：" + 1);
                                                                            tc.accountRecordDao.save(ar2);
                                                                        }

                                                                        // 查看当前模式当前产品当前人 是否 我要自提过
                                                                        List<CollageInRecord3> getCir3List_1 = tc.collageInRecord3Dao.queryCir3ByIds_1_1(cir3.getModeId(), cir3.getProductId(), cir3.getUserId());
                                                                        if(getCir3List_1.size() > 0){
                                                                            temp = false;
                                                                            // 已经提过货了
                                                                        }else{
                                                                            temp = true;
                                                                            // 需要强制一个代售成为发货单
                                                                        }

                                                                    }
                                                                    cir3.setIsTake("0"); // 不能提货
                                                                    cir3.setEndTime(orders.getUpdateTime()); // set第13个的时间
                                                                    tc.collageInRecord3Dao.update(cir3);
                                                                }

                                                                if(temp){
                                                                    if(getCir3_1 != null){
                                                                        for(int b = 0; b < cir3List_copy.size(); b++){
                                                                            CollageInRecord3 cir3_1 = cir3List_copy.get(b);
                                                                            if(getCir3_1.getId() == cir3_1.getId()) {
                                                                                // 修改订单信息
                                                                                Orders orders1_1 = tc.ordersDao.queryByOrderNo(cir3_1.getOrderNo());
                                                                                if(orders1_1 != null){
                                                                                    orders1_1.setDealStatus("1"); // 待发货
                                                                                    orders1_1.setRemarks("累计抽中已满三次, 当下返还一个代售并且再发货一个代售");
                                                                                    tc.ordersDao.update(orders1_1);

                                                                                    cir3_1.setStatus("1"); // 已返还
                                                                                    cir3_1.setEstimateReturn(time); // 当天再返回一个代售
                                                                                    tc.collageInRecord3Dao.update(cir3_1);
                                                                                }
                                                                            }
                                                                        }
                                                                    }else{
                                                                        System.err.println("没有代售了!!!");
                                                                    }
                                                                }

                                                            }

                                                            // 将累计的总体修改为已退
                                                            CollageInRecord3Copy isCir3Copy = tc.collageInRecord3CopyDao.queryByIds(orders.getCollageModeId(), orders.getProductId(), orders.getOrderPersonId());
                                                            if(isCir3Copy != null){
                                                                isCir3Copy.setUpdateTime(new Date());
                                                                isCir3Copy.setStatus("1");
                                                                tc.collageInRecord3CopyDao.update(isCir3Copy);
                                                            }

                                                            // 将剩余的代售进行定时，每月返回一个
                                                            List<CollageInRecord3> cir3List_1 = tc.collageInRecord3Dao.queryCir3ByIds_2(orders.getCollageModeId(), orders.getProductId(), orders.getOrderPersonId());
                                                            int size = cir3List_1.size();
                                                            if(size > 0){
                                                                String time = df.format(orders.getUpdateTime());
                                                                TimeUtil timeUtil = new TimeUtil();
                                                                String[] strings = timeUtil.getNextMonth_for(time, size);
                                                                for(int z = 0; z < strings.length; z++){
                                                                    for(int x = 0; x < size; x++){
                                                                        if(z == x){
                                                                            CollageInRecord3 cir = cir3List_1.get(x);
                                                                            cir.setEstimateReturn(strings[z]);
                                                                            tc.collageInRecord3Dao.update(cir);
                                                                        }
                                                                    }
                                                                }
                                                            }

                                                            // 创建第13个
                                                            CollageInRecord3 newCir3 = new CollageInRecord3();
                                                            newCir3.setModeId(orders.getCollageModeId());
                                                            newCir3.setProductId(orders.getProductId());
                                                            newCir3.setUserId(orders.getOrderPersonId());
                                                            newCir3.setStatus("0");
                                                            newCir3.setIsTake("1");
                                                            newCir3.setOrderNo(orders.getOrderNo());
                                                            newCir3.setPayAmount(orders.getPayAmount());
                                                            newCir3.setUpdateTime(orders.getUpdateTime());
                                                            tc.collageInRecord3Dao.save(newCir3);

                                                        }

                                                    }else{
                                                        // 第一次拼中当前产品
                                                        CollageInRecord3 newCir4 = new CollageInRecord3();
                                                        newCir4.setModeId(orders.getCollageModeId());
                                                        newCir4.setProductId(orders.getProductId());
                                                        newCir4.setUserId(orders.getOrderPersonId());
                                                        newCir4.setStatus("0"); // 未返还
                                                        newCir4.setIsTake("1"); // 还能提货
                                                        newCir4.setOrderNo(orders.getOrderNo());
                                                        newCir4.setPayAmount(orders.getPayAmount());
                                                        newCir4.setUpdateTime(orders.getUpdateTime());
                                                        tc.collageInRecord3Dao.save(newCir4);

                                                        // 查看CollageInRecord3Copy当前模式当前产品当前人一共累计有多少
                                                        CollageInRecord3Copy cir3Copy = tc.collageInRecord3CopyDao.queryByIds(orders.getCollageModeId(), orders.getProductId(), orders.getOrderPersonId());
                                                        if(cir3Copy == null){
                                                            // 创建并累计一次
                                                            CollageInRecord3Copy newCir3Copy = new CollageInRecord3Copy();
                                                            newCir3Copy.setModeId(orders.getCollageModeId());
                                                            newCir3Copy.setProductId(orders.getProductId());
                                                            newCir3Copy.setUserId(orders.getOrderPersonId());
                                                            newCir3Copy.setTotalQuantity(1);
                                                            newCir3Copy.setStatus("0");
                                                            newCir3Copy.setPayAmount(orders.getPayAmount());
                                                            newCir3Copy.setUpdateTime(new Date());
                                                            tc.collageInRecord3CopyDao.save(newCir3Copy);
                                                        }else{
                                                            // 直接累计一次
                                                            cir3Copy.setTotalQuantity(cir3Copy.getTotalQuantity() + 1);
                                                            cir3Copy.setPayAmount(cir3Copy.getPayAmount().add(orders.getPayAmount()));
                                                            cir3Copy.setUpdateTime(new Date());
                                                            tc.collageInRecord3CopyDao.update(cir3Copy);
                                                        }
                                                    }


                                                    // TODO 判断当前拼团模式中是否存在满足十二次的 // TODO 将12改成3
                                                    CollageInRecord3Copy cir3Copy12 = collageInRecord3CopyDao.queryByIds_1(orders.getCollageModeId(), orders.getProductId(), orders.getOrderPersonId());
                                                    if(cir3Copy12 != null){
                                                        // 满足十二次了
                                                        List<CollageInRecord3> getCir3List = tc.collageInRecord3Dao.queryCir3ByIds_1(cir3Copy12.getModeId(), cir3Copy12.getProductId(), cir3Copy12.getUserId());
                                                        if(getCir3List.size() > 0){
                                                            CollageInRecord3 getCir3 = getCir3List.get(0);
                                                            CollageInRecord3 getCir3_1 = null;
                                                            if(getCir3List.size() > 1){
                                                                getCir3_1 = getCir3List.get(1);
                                                            }
                                                            String time = df.format(orders.getUpdateTime());
                                                            // 当前模式当前产品当前人的所有中条数
                                                            List<CollageInRecord3> cir3List_1 = tc.collageInRecord3Dao.queryCir3ByIds(cir3Copy12.getModeId(), cir3Copy12.getProductId(), cir3Copy12.getUserId());
                                                            int cir3Size_1 = cir3List_1.size();
                                                            List<CollageInRecord3> cir3List_1_1 = cir3List_1;
                                                            boolean temp_1 = false;
                                                            if(cir3Size_1 > 0){
                                                                for(int h = 0; h < cir3Size_1; h++){
                                                                    CollageInRecord3 cir3_1 = cir3List_1.get(h);
                                                                    if(getCir3.getId() == cir3_1.getId()){

                                                                        cir3_1.setStatus("1"); // 已返还
                                                                        cir3_1.setEstimateReturn(time); // 当天返回一个代售

                                                                        // 退钱
                                                                        Account acc3 = tc.accountDao.queryAccountByUserId(cir3_1.getUserId());
                                                                        acc3.setVoucherBalance(acc3.getVoucherBalance().add(cir3_1.getPayAmount()));
                                                                        tc.accountDao.update(acc3);

                                                                        AccountRecord ar = new AccountRecord();
                                                                        ar.setIntoUserId(cir3_1.getUserId());
                                                                        ar.setUpdateTime(new Date());
                                                                        ar.setType("0");
                                                                        ar.setAmount(cir3_1.getPayAmount());
                                                                        ar.setNowAmount(acc3.getVoucherBalance());
                                                                        ar.setSign("1");
                                                                        ar.setOrderNo(orders.getOrderNo());
                                                                        // TODO 将12改成3
                                                                        ar.setInfo(tc.customerDao.queryById(cir3_1.getUserId()).getAgentCode() + "参团成功! 累计抽中已满三次, 当下返回一个代售：" + cir3_1.getPayAmount());
                                                                        tc.accountRecordDao.save(ar);

                                                                        // 修改订单信息
                                                                        Orders orders1 = tc.ordersDao.queryByOrderNo(cir3_1.getOrderNo());
                                                                        if(orders1 != null){
                                                                            orders1.setDealStatus("4"); // 交易关闭
                                                                            orders1.setRefundStatus("2"); // 退款成功
                                                                            orders1.setPayStatus("3"); // 已退款
                                                                            tc.ordersDao.update(orders1);
                                                                        }

                                                                        // 送共享券
                                                                        UserCoupon userCoupon = tc.userCouponDao.queryByUserIdAndCouponId(cir3_1.getUserId(), 2);
                                                                        if(userCoupon != null){
                                                                            userCoupon.setQuantity(userCoupon.getQuantity() + 1);
                                                                            userCoupon.setUpdateTime(new Date());
                                                                            tc.userCouponDao.update(userCoupon);

                                                                            AccountRecord ar1 = new AccountRecord();
                                                                            ar1.setIntoUserId(cir3_1.getUserId());
                                                                            ar1.setUpdateTime(new Date());
                                                                            ar1.setType("6");
                                                                            ar1.setAmount(new BigDecimal(1));
                                                                            ar1.setNowAmount(new BigDecimal(userCoupon.getQuantity()));
                                                                            ar1.setSign("1");
                                                                            // TODO 将12改成3
                                                                            ar1.setInfo(tc.customerDao.queryById(cir3_1.getUserId()).getAgentCode() + "参团成功! 累计抽中已满三次, 当下返还代售时赠送共享券：" + 1);
                                                                            tc.accountRecordDao.save(ar1);
                                                                        }else{
                                                                            UserCoupon newUc = new UserCoupon();
                                                                            newUc.setQuantity(1);
                                                                            newUc.setUpdateTime(new Date());
                                                                            newUc.setCouponId(2);
                                                                            newUc.setUserId(cir3_1.getUserId());
                                                                            tc.userCouponDao.save(newUc);

                                                                            AccountRecord ar2 = new AccountRecord();
                                                                            ar2.setIntoUserId(cir3_1.getUserId());
                                                                            ar2.setUpdateTime(new Date());
                                                                            ar2.setType("6");
                                                                            ar2.setAmount(new BigDecimal(1));
                                                                            ar2.setNowAmount(new BigDecimal(newUc.getQuantity()));
                                                                            ar2.setSign("1");
                                                                            // TODO 将12改成3
                                                                            ar2.setInfo(tc.customerDao.queryById(cir3_1.getUserId()).getAgentCode() + "参团成功! 累计抽中已满三次, 当下返还代售时赠送共享券：" + 1);
                                                                            tc.accountRecordDao.save(ar2);
                                                                        }
                                                                        List<CollageInRecord3> getCir3List_1 = tc.collageInRecord3Dao.queryCir3ByIds_1_1(cir3_1.getModeId(), cir3_1.getProductId(), cir3_1.getUserId());
                                                                        if(getCir3List_1.size() > 0){
                                                                            temp_1 = false;
                                                                            // 已经提过货了
                                                                        }else{
                                                                            temp_1 = true;
                                                                            // 需要强制一个代售成为发货单
                                                                        }
                                                                    }
                                                                    cir3_1.setIsTake("0"); // 不能提货
                                                                    cir3_1.setEndTime(orders.getUpdateTime()); // set第13个的时间
                                                                    tc.collageInRecord3Dao.update(cir3_1);

                                                                }

                                                                if(temp_1){
                                                                    if(getCir3_1 != null){
                                                                        for(int c = 0; c < cir3List_1_1.size(); c++){
                                                                            CollageInRecord3 cir3_1_1 = cir3List_1_1.get(c);
                                                                            if(getCir3_1.getId() == cir3_1_1.getId()) {
                                                                                // 修改订单信息
                                                                                Orders orders1_1_1 = tc.ordersDao.queryByOrderNo(cir3_1_1.getOrderNo());
                                                                                if(orders1_1_1 != null){
                                                                                    orders1_1_1.setDealStatus("1"); // 待发货
                                                                                    orders1_1_1.setRemarks("累计抽中已满三次, 当下返还一个代售并且再发货一个代售");
                                                                                    tc.ordersDao.update(orders1_1_1);

                                                                                    cir3_1_1.setStatus("1"); // 已返还
                                                                                    cir3_1_1.setEstimateReturn(time); // 当天返回一个代售
                                                                                    tc.collageInRecord3Dao.update(cir3_1_1);
                                                                                }
                                                                            }
                                                                        }
                                                                    }else{
                                                                        System.err.println("没有代售了!!!");
                                                                    }
                                                                }

                                                            }
                                                        }

                                                        // 将累计的总体修改为已退
                                                        CollageInRecord3Copy isCir3Copy = tc.collageInRecord3CopyDao.queryByIds(cir3Copy12.getModeId(), cir3Copy12.getProductId(), cir3Copy12.getUserId());
                                                        if(isCir3Copy != null){
                                                            isCir3Copy.setUpdateTime(new Date());
                                                            isCir3Copy.setStatus("1");
                                                            tc.collageInRecord3CopyDao.update(isCir3Copy);
                                                        }

                                                        // 将剩余的代售进行定时，每月返回一个
                                                        List<CollageInRecord3> cir3List_1 = tc.collageInRecord3Dao.queryCir3ByIds_2(cir3Copy12.getModeId(), cir3Copy12.getProductId(), cir3Copy12.getUserId());
                                                        int size = cir3List_1.size();
                                                        if(size > 0){
                                                            String time = df.format(orders.getUpdateTime());
                                                            TimeUtil timeUtil = new TimeUtil();
                                                            String[] strings = timeUtil.getNextMonth_for(time, size);
                                                            for(int z = 0; z < strings.length; z++){
                                                                for(int x = 0; x < size; x++){
                                                                    if(z == x){
                                                                        CollageInRecord3 cir = cir3List_1.get(x);
                                                                        cir.setEstimateReturn(strings[z]);
                                                                        tc.collageInRecord3Dao.update(cir);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }

                                                }

                                                /* 速度与激情 自动成团暂时去掉

                                                // 速度与激情
                                                if(orders.getCollageModeId() == 20){
                                                    // 判断当前产品是否满六次，执行全额退款
                                                    CollageInRecord20 cir20 = tc.collageInRecord20Dao.queryCir20ByIds(orders.getCollageModeId(), orders.getOrderPersonId());
                                                    if (cir20 == null) {
                                                        // 第一次中当前产品
                                                        CollageInRecord20 newCir20 = new CollageInRecord20();
                                                        newCir20.setModeId(orders.getCollageModeId());
                                                        newCir20.setUserId(orders.getOrderPersonId());
                                                        newCir20.setStatus("0");
                                                        newCir20.setTotalQuantity(1);
                                                        newCir20.setPayAmount(orders.getPayAmount());
                                                        newCir20.setUpdateTime(new Date());
                                                        tc.collageInRecord20Dao.save(newCir20);
                                                    } else {
                                                        // TODO 将6改成3
                                                        if (cir20.getTotalQuantity() < 3) {
                                                            // 继续累积
                                                            cir20.setPayAmount(cir20.getPayAmount().add(orders.getPayAmount()));
                                                            cir20.setTotalQuantity(cir20.getTotalQuantity() + 1);
                                                            cir20.setUpdateTime(new Date());
                                                            tc.collageInRecord20Dao.update(cir20);
                                                        } else {
                                                            // 满足六次，退钱六次的钱
                                                            cir20.setStatus("1");
                                                            cir20.setUpdateTime(new Date());
                                                            tc.collageInRecord20Dao.update(cir20);

                                                            Account accCir = tc.accountDao.queryAccountByUserId(orders.getOrderPersonId());
                                                            accCir.setVoucherBalance(accCir.getVoucherBalance().add(cir20.getPayAmount()));
                                                            tc.accountDao.update(accCir);

                                                            AccountRecord arCir = new AccountRecord();
                                                            arCir.setIntoUserId(orders.getOrderPersonId());
                                                            arCir.setUpdateTime(new Date());
                                                            arCir.setType("0");
                                                            arCir.setAmount(cir20.getPayAmount());
                                                            arCir.setNowAmount(accCir.getVoucherBalance());
                                                            arCir.setSign("1");
                                                            arCir.setOrderNo(orders.getOrderNo());
                                                            // TODO 将6改成3
                                                            arCir.setInfo(customer.getAgentCode() + "参团成功! 累积拼中产品三次,获得广告补贴为：" + cir20.getPayAmount());
                                                            tc.accountRecordDao.save(arCir);

                                                            // 送共享券
                                                            UserCoupon userCoupon = tc.userCouponDao.queryByUserIdAndCouponId(cir20.getUserId(), 2);
                                                            if (userCoupon != null) {
                                                                userCoupon.setQuantity(userCoupon.getQuantity() + 1);
                                                                userCoupon.setUpdateTime(new Date());
                                                                tc.userCouponDao.update(userCoupon);

                                                                AccountRecord ar1 = new AccountRecord();
                                                                ar1.setIntoUserId(cir20.getUserId());
                                                                ar1.setUpdateTime(new Date());
                                                                ar1.setType("6");
                                                                ar1.setAmount(new BigDecimal(1));
                                                                ar1.setNowAmount(new BigDecimal(userCoupon.getQuantity()));
                                                                ar1.setSign("1");
                                                                // TODO 将6改成3
                                                                ar1.setInfo(tc.customerDao.queryById(cir20.getUserId()).getAgentCode() + "参团成功! 累积拼中产品三次,退款总金额并赠送共享券：" + 1);
                                                                tc.accountRecordDao.save(ar1);
                                                            } else {
                                                                UserCoupon newUc = new UserCoupon();
                                                                newUc.setQuantity(1);
                                                                newUc.setUpdateTime(new Date());
                                                                newUc.setCouponId(2);
                                                                newUc.setUserId(cir20.getUserId());
                                                                tc.userCouponDao.save(newUc);

                                                                AccountRecord ar2 = new AccountRecord();
                                                                ar2.setIntoUserId(cir20.getUserId());
                                                                ar2.setUpdateTime(new Date());
                                                                ar2.setType("6");
                                                                ar2.setAmount(new BigDecimal(1));
                                                                ar2.setNowAmount(new BigDecimal(newUc.getQuantity()));
                                                                ar2.setSign("1");
                                                                // TODO 将6改成3
                                                                ar2.setInfo(tc.customerDao.queryById(cir20.getUserId()).getAgentCode() + "参团成功! 累积拼中产品三次,退款总金额并赠送共享券：" + 1);
                                                                tc.accountRecordDao.save(ar2);
                                                            }

                                                            // 存下第七次
                                                            CollageInRecord20 newCir7 = new CollageInRecord20();
                                                            newCir7.setModeId(orders.getCollageModeId());
                                                            newCir7.setUserId(orders.getOrderPersonId());
                                                            newCir7.setStatus("0");
                                                            newCir7.setTotalQuantity(1);
                                                            newCir7.setPayAmount(orders.getPayAmount());
                                                            newCir7.setUpdateTime(new Date());
                                                            tc.collageInRecord20Dao.save(newCir7);
                                                        }
                                                    }
                                                }

                                                 */

                                            }
                                        }
                                    }else{
                                        if("1".equals(cu.getFiction())){
                                            // 虚拟用户退款

                                        }else{
                                            // 正式用户退款，判断是否勾选拼中为止
                                            Orders orders = tc.ordersDao.queryByIsO(cu.getUserId(), mp.getId());
                                            if(orders != null){
                                                Product product = tc.productDao.queryById(orders.getProductId());
                                                Customer customer = tc.customerDao.queryById(orders.getOrderPersonId());
                                                BigDecimal couponAmount = null;
                                                if(orders.getCouponAmount() == null){
                                                    couponAmount = new BigDecimal(0);
                                                }else{
                                                    couponAmount = orders.getCouponAmount();
                                                }
                                                if("1".equals(orders.getLastCollage())){

                                                    // TODO 当前订单转移到 OrdersCopy
                                                    OrdersCopy oc = new OrdersCopy();
                                                    oc.setId(orders.getId());
                                                    oc.setOrderNo(orders.getOrderNo());
                                                    oc.setTradeNo(orders.getTradeNo());
                                                    oc.setTotalAmount(orders.getTotalAmount());
                                                    oc.setFreight(orders.getFreight());
                                                    oc.setCollageModeId(orders.getCollageModeId());
                                                    oc.setOrderPersonId(orders.getOrderPersonId());
                                                    oc.setLastCollage(orders.getLastCollage());
                                                    oc.setConsigneeName(orders.getConsigneeName());
                                                    oc.setConsigneePhone(orders.getConsigneePhone());
                                                    oc.setAddress(orders.getAddress());
                                                    oc.setUpdateTime(new Date());
                                                    oc.setPayAmount(orders.getPayAmount());
                                                    oc.setCouponAmount(orders.getCouponAmount());
                                                    oc.setModeProductId(orders.getModeProductId());
                                                    oc.setProductId(orders.getProductId());
                                                    oc.setCouponId(1);
                                                    oc.setSkuId(tc.orderDetailsDao.queryByOrderNo(orders.getOrderNo()).getRelationId());
                                                    if(couponAmount.doubleValue() > 0){
                                                        int quantity1 = (int) (couponAmount.doubleValue() / 9.9);
                                                        oc.setCouponQuantity(quantity1);
                                                    }else{
                                                        oc.setCouponQuantity(0);
                                                    }
                                                    tc.ordersCopyDao.save(oc);

                                                    // 给个红包
//                                                    Account accN = tc.accountDao.queryAccountByUserId(orders.getOrderPersonId());
//                                                    accN.setVoucherBalance(accN.getVoucherBalance().add(product.getVoucher()));
//                                                    tc.accountDao.update(accN);
//
//                                                    AccountRecord ar1_2 = new AccountRecord();
//                                                    ar1_2.setIntoUserId(orders.getOrderPersonId());
//                                                    ar1_2.setUpdateTime(new Date());
//                                                    ar1_2.setType("0");
//                                                    ar1_2.setAmount(product.getVoucher());
//                                                    ar1_2.setNowAmount(accN.getVoucherBalance());
//                                                    ar1_2.setSign("1");
//                                                    ar1_2.setOrderNo(orders.getOrderNo());
//                                                    ar1_2.setInfo(customer.getAgentCode() + "参团成功! 未抽中,送您余额：" + product.getVoucher());
//                                                    tc.accountRecordDao.save(ar1_2);

                                                    FailRedPack frp = new FailRedPack();
                                                    frp.setOrderNo(orders.getOrderNo());
                                                    frp.setPrizeMoney(product.getVoucher());
                                                    frp.setStatus("2"); // 未开红包
                                                    frp.setUpdateTime(new Date());
                                                    frp.setUserId(orders.getOrderPersonId());
                                                    frp.setMode("0");
                                                    tc.failRedPackDao.save(frp);

                                                    // 处理参与拼团后的利润分配
                                                    // tc.ordersCopyService.handleCollageProfit(orders.getOrderPersonId(), product.getVoucher(), orders.getOrderNo());

                                                }else{
                                                    // 执行退款(退到余额)退券 把每个人领取到的红包进行分润
                                                    orders.setDealStatus("4"); // 交易关闭
                                                    orders.setRefundStatus("2"); // 退款成功
                                                    orders.setPayStatus("3"); // 已退款
                                                    tc.ordersDao.update(orders);

                                                    Account acc = tc.accountDao.queryAccountByUserId(orders.getOrderPersonId());
                                                    acc.setVoucherBalance(acc.getVoucherBalance().add(orders.getPayAmount()));
                                                    tc.accountDao.update(acc);

                                                    AccountRecord ar1_1 = new AccountRecord();
                                                    ar1_1.setIntoUserId(orders.getOrderPersonId());
                                                    ar1_1.setUpdateTime(new Date());
                                                    ar1_1.setType("0");
                                                    ar1_1.setAmount(orders.getPayAmount());
                                                    ar1_1.setNowAmount(acc.getVoucherBalance());
                                                    ar1_1.setSign("1");
                                                    ar1_1.setOrderNo(orders.getOrderNo());
                                                    ar1_1.setInfo(customer.getAgentCode() + "参团成功! 未抽中,支付金额退回至余额：" + orders.getPayAmount());
                                                    tc.accountRecordDao.save(ar1_1);

                                                    if(couponAmount.doubleValue() > 0){
                                                        int quantity1 = (int) (couponAmount.doubleValue() / 9.9);
                                                        if(quantity1 > 0){
                                                            UserCoupon uc = tc.userCouponDao.queryByUserIdAndCouponId(orders.getOrderPersonId(), 1);
                                                            if(uc != null){
                                                                uc.setQuantity(uc.getQuantity() + quantity1);
                                                                tc.userCouponDao.update(uc);

                                                                AccountRecord ar2 = new AccountRecord();
                                                                ar2.setIntoUserId(orders.getOrderPersonId());
                                                                ar2.setUpdateTime(new Date());
                                                                ar2.setType("5"); // 拼团券
                                                                ar2.setAmount(new BigDecimal(quantity1));
                                                                ar2.setNowAmount(new BigDecimal(uc.getQuantity()));
                                                                ar2.setSign("1");
                                                                ar2.setOrderNo(orders.getOrderNo());
                                                                ar2.setInfo(customer.getAgentCode() + "参团成功! 未抽中,拼团券退回：" + quantity1);
                                                                tc.accountRecordDao.save(ar2);
                                                            }
                                                        }
                                                    }

                                                    // acc.setVoucherBalance(acc.getVoucherBalance().add(product.getVoucher()));
                                                    // tc.accountDao.update(acc);

//                                                    AccountRecord ar1_2 = new AccountRecord();
//                                                    ar1_2.setIntoUserId(orders.getOrderPersonId());
//                                                    ar1_2.setUpdateTime(new Date());
//                                                    ar1_2.setType("0");
//                                                    ar1_2.setAmount(product.getVoucher());
//                                                    ar1_2.setNowAmount(acc.getVoucherBalance());
//                                                    ar1_2.setSign("1");
//                                                    ar1_2.setOrderNo(orders.getOrderNo());
//                                                    ar1_2.setInfo(customer.getAgentCode() + "参团成功! 未抽中,送您余额：" + product.getVoucher());
//                                                    tc.accountRecordDao.save(ar1_2);

                                                    FailRedPack frp = new FailRedPack();
                                                    frp.setOrderNo(orders.getOrderNo());
                                                    frp.setPrizeMoney(product.getVoucher());
                                                    frp.setStatus("2"); // 未开红包
                                                    frp.setUpdateTime(new Date());
                                                    frp.setUserId(orders.getOrderPersonId());
                                                    frp.setMode("0");
                                                    tc.failRedPackDao.save(frp);

                                                    // 处理参与拼团后的利润分配
                                                    // tc.ordersCopyService.handleCollageProfit(orders.getOrderPersonId(), product.getVoucher(), orders.getOrderNo());

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }else{
                        System.err.println("没有团 需要自动完成++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    }
                }
            }
        }
    }


    // 每天凌晨0点整进行奖池分账（分昨日的，根据业绩占比分）延迟2秒
    //@Scheduled(cron = "2 0 0 * * ?")
    public void TimingBusiness13() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.DAY_OF_MONTH, -1);
        String date1 = sdf.format(now.getTime());
        // 获取昨日奖池
        GoodsPrizePool app = tc.goodsPrizePoolDao.queryGoodsPrizePoolByToday(date1);
        if(app != null) {
            String time = sdf.format(new Date());
            GoodsPrizePool app1 = tc.goodsPrizePoolDao.queryGoodsPrizePoolByToday(time);
            if (app1 == null) {
                // 创建今日奖池
                GoodsPrizePool newApp = new GoodsPrizePool();
                newApp.setUpdateTime(new Date());
                newApp.setPrizePoolAmount(new BigDecimal(0));
                tc.goodsPrizePoolDao.save(newApp);
            }
            // 昨日总业绩
            BigDecimal total = tc.goodsSplitAccountDao.queryByTotal(date1);
            // 订单业绩详情
            List<GoodsSplitAccount> gsaList = tc.goodsSplitAccountDao.queryByRecord(date1);
            if(gsaList.size() > 0){
                for(GoodsSplitAccount gsa : gsaList){
                    BigDecimal div = gsa.getSplitAccount().divide(total, 4, BigDecimal.ROUND_HALF_UP);
                    BigDecimal mul = div.multiply(app.getPrizePoolAmount()).setScale(2, BigDecimal.ROUND_UP);
                    Account account = tc.accountDao.queryAccountByUserId(gsa.getParentId());
                    account.setVoucherBalance(account.getVoucherBalance().add(mul));
                    tc.accountDao.update(account);
                    AccountRecord ar = new AccountRecord();
                    ar.setIntoUserId(gsa.getParentId());
                    ar.setOrderNo(gsa.getTradeNo());
                    ar.setInfo(tc.customerDao.queryById(gsa.getOrderPersonId()).getAgentCode() + "购买商品，您获得推荐利润（按奖池占比分配）：" + mul);
                    ar.setUpdateTime(new Date());
                    ar.setSign("1");
                    ar.setType("0");
                    ar.setAmount(mul);
                    ar.setNowAmount(account.getVoucherBalance());
                    tc.accountRecordDao.save(ar);
                    // 修改分润状态
                    gsa.setStatus("1");
                    gsa.setMoney(mul); // 当前订单根据占比分到的钱
                    tc.goodsSplitAccountDao.update(gsa);
                }
            }
        }
    }


    // 每天晚上十点处理100/200专区的团 延迟2分钟
    //@Scheduled(cron = "0 2 22 * * ?")
    public void TimingBusiness14() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String today = sdf.format(date);
        // 获取正在进行中的100/200的团
        List<ModeProduct> mpList = tc.modeProductDao.queryByStatus_30_40();
        if(mpList.size() > 0){
            for(int i = 0; i < mpList.size(); i++){
                ModeProduct mp = mpList.get(i);
                mp.setStatus("1");
                tc.modeProductDao.update(mp); // 团成功
                // 获取当前团ID的所有订单
                List<Orders> ordersList = tc.ordersDao.queryOrdersByCollageProductId(mp.getId());
                if(ordersList.size() > 0){
                    for(int j = 0; j < ordersList.size(); j++){
                        Orders o = ordersList.get(j);
                        Customer cu = tc.customerDao.queryById(o.getOrderPersonId());
                        OrderDetails od = tc.orderDetailsDao.queryByOrderNo(o.getOrderNo());
                        Product p = tc.productDao.queryById(od.getProductId());
                        if ("1".equals(cu.getIsMember())) {
                            // 会员今日每中 直接一波流出局
                            // todo 那29元 修改为14
                            BigDecimal profit = BigDecimal.valueOf(14);
                            if (p.getCollageModeId() == 40) {
                                // todo 拿走58元 修改为28
                                profit = BigDecimal.valueOf(28);
                            }
                            CollageTotalVoucher ctv = tc.collageTotalVoucherDao.queryByIdsAndTodayTime(cu.getId(), p.getCollageModeId(), today);
                            if (ctv == null) {
                                CollageTotalVoucher newCtv = new CollageTotalVoucher();
                                newCtv.setAmount(profit);
                                newCtv.setModeId(p.getCollageModeId());
                                newCtv.setStatus("0");
                                newCtv.setUpdateTime(date);
                                newCtv.setUserId(cu.getId());
                                tc.collageTotalVoucherDao.save(newCtv);
                                // 分润
                                tc.ordersService.handleCollageProfit30(cu.getId(), profit, o.getOrderNo());
                            }

                            // 获取累计中奖记录数据
                            CollageInRecord30 cir30 = tc.collageInRecord30Dao.queryByModeIdAndUserId(o.getCollageModeId(), cu.getId());
                            if (cir30 == null) {
                                // 创建cir30
                                CollageInRecord30 newCir30 = new CollageInRecord30();
                                newCir30.setModeId(o.getCollageModeId());
                                newCir30.setUserId(cu.getId());
                                newCir30.setStatus("0");
                                newCir30.setTotalQuantity(1);
                                newCir30.setUpdateTime(date);
                                newCir30.setPayAmount(o.getPayAmount());
                                tc.collageInRecord30Dao.save(newCir30);
                                // 添加详情
                                CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                newCir30Copy.setIsTack("1"); // 提了
                                newCir30Copy.setCir30Id(newCir30.getId());
                                newCir30Copy.setOrderNo(o.getOrderNo());
                                newCir30Copy.setPayAmount(o.getPayAmount());
                                tc.collageInRecord30CopyDao.save(newCir30Copy);
                                // 发货
                                o.setDealStatus("1");
                                tc.ordersDao.update(o);
                            } else {
                                // 判断是否需要返钱
                                if (cir30.getTotalQuantity() == 5) {
                                    // 开始退钱，退未提货的钱
                                    Account cuAcc = tc.accountDao.queryAccountByUserId(cu.getId());
                                    Double d = 0.0;
                                    List<CollageInRecord30Copy> cir30CopyList = tc.collageInRecord30CopyDao.queryByCir30Id(cir30.getId());
                                    if (cir30CopyList.size() > 0) {
                                        for (CollageInRecord30Copy cir30Copy : cir30CopyList) {
                                            cir30Copy.setIsTack("1"); // 修改为已提货
                                            d += cir30Copy.getPayAmount().doubleValue();
                                            tc.collageInRecord30CopyDao.update(cir30Copy);
                                            Orders orders = tc.ordersDao.queryByOrderNo(cir30Copy.getOrderNo());
                                            orders.setDealStatus("3"); // 已完成
                                            orders.setConfirmTime(new Date());
                                            orders.setRemarks("拼团专区内累计拼中六次! 当前订单自动完成");
                                            tc.ordersDao.update(orders);
                                        }
                                    }
                                    cuAcc.setVoucherBalance(cuAcc.getVoucherBalance().add(BigDecimal.valueOf(d))); //余额
                                    tc.accountDao.update(cuAcc);
                                    // 加个ar记录
                                    AccountRecord ar = new AccountRecord();
                                    ar.setIntoUserId(cu.getId());
                                    ar.setUpdateTime(date);
                                    ar.setType("0");
                                    ar.setAmount(BigDecimal.valueOf(d));
                                    ar.setNowAmount(cuAcc.getVoucherBalance());
                                    ar.setSign("1");
                                    ar.setOrderNo(o.getOrderNo());
                                    ar.setInfo("拼团专区内累计拼中六次! 广告补贴：" + d);
                                    tc.accountRecordDao.save(ar);
                                    // 改为退完
                                    cir30.setStatus("1"); // 已退
                                    tc.collageInRecord30Dao.update(cir30);
                                    // 把第六次做为新的第一次并发货
                                    // 发货
                                    o.setDealStatus("1");
                                    tc.ordersDao.update(o);
                                    // 创建cir30
                                    CollageInRecord30 newCir30 = new CollageInRecord30();
                                    newCir30.setModeId(o.getCollageModeId());
                                    newCir30.setUserId(cu.getId());
                                    newCir30.setStatus("0");
                                    newCir30.setTotalQuantity(1);
                                    newCir30.setUpdateTime(date);
                                    newCir30.setPayAmount(o.getPayAmount());
                                    tc.collageInRecord30Dao.save(newCir30);
                                    // 添加详情
                                    CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                    newCir30Copy.setIsTack("1"); // 提了
                                    newCir30Copy.setCir30Id(newCir30.getId());
                                    newCir30Copy.setOrderNo(o.getOrderNo());
                                    newCir30Copy.setPayAmount(o.getPayAmount());
                                    tc.collageInRecord30CopyDao.save(newCir30Copy);
                                } else {
                                    // 继续累计
                                    cir30.setTotalQuantity(cir30.getTotalQuantity() + 1);
                                    cir30.setPayAmount(cir30.getPayAmount().add(o.getPayAmount()));
                                    tc.collageInRecord30Dao.update(cir30);
                                    // 添加详情
                                    CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                    newCir30Copy.setIsTack("0"); // 没有提
                                    newCir30Copy.setCir30Id(cir30.getId());
                                    newCir30Copy.setOrderNo(o.getOrderNo());
                                    newCir30Copy.setPayAmount(o.getPayAmount());
                                    tc.collageInRecord30CopyDao.save(newCir30Copy);
                                    // 代售
                                    o.setDealStatus("7");
                                    tc.ordersDao.update(o);
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    // 每天晚上十点半重置100/200专区的参团券
    //@Scheduled(cron = "0 25 22 * * ?")
    public void TimingBusiness15() {
        List<UserCoupon> ucList = tc.userCouponDao.queryByCouponId();
        if(ucList.size() > 0){
            for(UserCoupon uc : ucList){
                uc.setQuantity(0); // 重置为0，明天签再获得
                tc.userCouponDao.update(uc);
            }
        }
    }


    // 每天上午八点半处理拼团余额是否有满足返还的
    //@Scheduled(cron = "0 40 8 * * ?")
    public void TimingBusiness16() {
        System.err.println("-------------------------------------->>>>>>>> 每天上午八点半处理拼团余额是否有满足返还的");
        List<TodayCollageVoucher> tcvList = tc.todayCollageVoucherDao.queryByStatus1();
        if(tcvList.size() > 0){
            for(TodayCollageVoucher tcv : tcvList){
                if(tcv.getModeId() == 30){
                    if(tcv.getRefundAmount().doubleValue() >= 29){
                        tcv.setRefundAmount(tcv.getRefundAmount().subtract(BigDecimal.valueOf(29)));
                        tc.todayCollageVoucherDao.update(tcv);
                        // 给个红包
                        FailRedPack frp = new FailRedPack();
                        frp.setUserId(tcv.getUserId());
                        frp.setUpdateTime(new Date());
                        frp.setStatus("2");
                        frp.setPrizeMoney(BigDecimal.valueOf(29));
                        frp.setMode("1");
                        tc.failRedPackDao.save(frp);
                        if(tcv.getRefundAmount().doubleValue() == 0){
                            tcv.setIsRefund("1"); // 已退还
                            tcv.setStatus("1");
                            tc.todayCollageVoucherDao.update(tcv);
                        }
                    }else{
                        tcv.setIsRefund("1"); // 已退还
                        tcv.setStatus("1");
                        tc.todayCollageVoucherDao.update(tcv);
                    }
                }else{
                    if(tcv.getRefundAmount().doubleValue() >= 58){
                        tcv.setRefundAmount(tcv.getRefundAmount().subtract(BigDecimal.valueOf(58)));
                        tc.todayCollageVoucherDao.update(tcv);
                        // 给个红包
                        FailRedPack frp = new FailRedPack();
                        frp.setUserId(tcv.getUserId());
                        frp.setUpdateTime(new Date());
                        frp.setStatus("2");
                        frp.setPrizeMoney(BigDecimal.valueOf(58));
                        frp.setMode("1");
                        tc.failRedPackDao.save(frp);
                        if(tcv.getRefundAmount().doubleValue() == 0){
                            tcv.setIsRefund("1"); // 已退还
                            tcv.setStatus("1");
                            tc.todayCollageVoucherDao.update(tcv);
                        }
                    }else{
                        tcv.setIsRefund("1"); // 已退还
                        tcv.setStatus("1");
                        tc.todayCollageVoucherDao.update(tcv);
                    }
                }
            }
        }
    }


    // 每天上午八点五十处理拼团利润是否有满足返还的
    //@Scheduled(cron = "0 50 8 * * ?")
    public void TimingBusiness16_1() throws ParseException {
        System.err.println("-------------------------------------->>>>>>>> 每天上午八点五十处理拼团利润是否有满足返还的");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        int num = 5; // 加的天数
        List<TodayCollageProfit> tcvList = tc.todayCollageProfitDao.queryByStatus();
        if(tcvList.size() > 0){
            for(TodayCollageProfit tcp : tcvList){
                String updateTime = format.format(tcp.getUpdateTime());
                Date currDate = format.parse(updateTime);
                Calendar ca = Calendar.getInstance();
                ca.setTime(currDate);
                ca.add(Calendar.DATE, num);
                currDate = ca.getTime();
                String endDate = sdf.format(currDate);
                if(endDate.equals(date)){
                    tcp.setStatus("1"); // 返还
                    tc.todayCollageProfitDao.update(tcp);
                    // 给个红包
                    FailRedPack frp = new FailRedPack();
                    frp.setUserId(tcp.getUserId());
                    frp.setUpdateTime(new Date());
                    frp.setStatus("2");
                    frp.setPrizeMoney(tcp.getAmount());
                    frp.setMode("1");
                    frp.setSign("佣金");
                    tc.failRedPackDao.save(frp);
                }
            }
        }
    }


    // 每天上午八点三十处理拼团利润返还五日前金额
    //@Scheduled(cron = "0 30 8 * * ?")
    public void TimingBusiness16_2() throws ParseException {
        System.err.println("-------------------------------------->>>>>>>> 每天上午八点三十处理拼团利润返还五日前金额");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        int num = 5; // 加的天数
        List<CollageTotalVoucher> tcvList = tc.collageTotalVoucherDao.queryByStatus();
        if(tcvList.size() > 0){
            for(CollageTotalVoucher tcv : tcvList){
                String updateTime = format.format(tcv.getUpdateTime());
                Date currDate = format.parse(updateTime);
                Calendar ca = Calendar.getInstance();
                ca.setTime(currDate);
                ca.add(Calendar.DATE, num);
                currDate = ca.getTime();
                String endDate = sdf.format(currDate);
                if(endDate.equals(date)){
                    tcv.setStatus("1"); // 返还
                    tc.collageTotalVoucherDao.update(tcv);
                    // 给个红包
                    FailRedPack frp = new FailRedPack();
                    frp.setUserId(tcv.getUserId());
                    frp.setUpdateTime(new Date());
                    frp.setStatus("2");
                    frp.setPrizeMoney(tcv.getAmount());
                    frp.setMode("1");
                    tc.failRedPackDao.save(frp);
                }
            }
        }
    }


    // 每天上午七点半，查看共享是否有需要复消的,执行复消
    //@Scheduled(cron = "0 30 7 * * ?")
    public void TimingBusiness17(){
        System.err.println("==================== >>>>>> 查看共享是否有需要复消的,执行复消");
        List<OrdersShareRepeat> osrList = tc.ordersShareRepeatDao.queryByStatus();
        if(osrList.size() > 0){
            for(OrdersShareRepeat osr : osrList){
                Account account = tc.accountDao.queryAccountByUserId(osr.getUserId());
                if(account.getFrozenIn().doubleValue() >= 369){
                    // 创建复消订单，并复消,处理一系列数据
                    boolean temp = tc.ordersShareRepeatService.handleCreateAndRepeatShareOrders(osr.getUserId());
                    if(temp){
                        osr.setStatus("1");
                        osr.setInfo("成功");
                    }else{
                        osr.setInfo("失败");
                    }
                    tc.ordersShareRepeatDao.update(osr);
                }
            }
        }
    }


    // 每隔2分钟执行一次 查看100/200团需要自动完成
    //@Scheduled(cron = "0 */6 * * * ?")
    public void TimingBusiness18() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获得当天年月日
        String time = sdf.format(new Date());
        // 早上十点半
        String time10 = time + " 10:30:00";
        Date date10 = sdf1.parse(time10);
        Calendar begin10 = Calendar.getInstance();
        begin10.setTime(date10);
        // 晚上九点半
        String time22 = time + " 21:50:00";
        Date date22 = sdf1.parse(time22);
        Calendar end22 = Calendar.getInstance();
        end22.setTime(date22);
        // 系统当前时间
        Date date = new Date();
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(date);
        // 当前时间为 2021-11-18 10:10:10   begin时间 2021-11-18 12:12:12  end时间2021-11-18 21:21:21
        if(nowTime.after(begin10) && nowTime.before(end22)){
            List<ModeProduct> mpList = tc.modeProductDao.queryByStatus_30_40(); // where status = 0 and (mode_id = 30 OR mode_id = 40)
            if(mpList.size() > 0){
                ModeProduct mp = mpList.get(0);
                CollageMode cm = tc.collageModeDao.queryById(mp.getModeId());
                Integer quantity = cm.getPeopleQuantity();
                List<CollageUser> cuList = tc.collageUserDao.queryByModeProductId(mp.getId());
                Integer rowSize = cuList.size();
                if (rowSize < quantity) {
                    System.err.println("往100/200团加入虚拟数据++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                    // 把当前团直接成功
                    mp.setStatus("1");
                    tc.modeProductDao.update(mp);
                    // 获取当前团的订单信息，处理哪些需要发货，哪些需要退款，哪些需要挪团
                    List<Orders> ordersList = tc.ordersDao.queryOrdersByCollageProductId(mp.getId());
                    int ordersSize = ordersList.size();
                    if(ordersSize > 0){
                        for(int u = 0; u < ordersSize; u++){
                            Orders o = ordersList.get(u);
                            Customer cu = tc.customerDao.queryById(o.getOrderPersonId());
                            OrderDetails od = tc.orderDetailsDao.queryByOrderNo(o.getOrderNo());
                            Product p = tc.productDao.queryById(od.getProductId());
                            if ("1".equals(cu.getIsMember())) {
                                // 会员今日每中 直接一波流出局
                                // todo 那29元 修改为14
                                BigDecimal profit = BigDecimal.valueOf(14);
                                if (p.getCollageModeId() == 40) {
                                    // todo 拿走58元 修改为28
                                    profit = BigDecimal.valueOf(28);
                                }
                                CollageTotalVoucher ctv = tc.collageTotalVoucherDao.queryByIdsAndTodayTime(cu.getId(), p.getCollageModeId(), time);
                                if (ctv == null) {
                                    CollageTotalVoucher newCtv = new CollageTotalVoucher();
                                    newCtv.setAmount(profit);
                                    newCtv.setModeId(p.getCollageModeId());
                                    newCtv.setStatus("0");
                                    newCtv.setUpdateTime(date);
                                    newCtv.setUserId(cu.getId());
                                    tc.collageTotalVoucherDao.save(newCtv);
                                    // 分润
                                    tc.ordersService.handleCollageProfit30(cu.getId(), profit, o.getOrderNo());
                                }

                                // 获取累计中奖记录数据
                                CollageInRecord30 cir30 = tc.collageInRecord30Dao.queryByModeIdAndUserId(o.getCollageModeId(), cu.getId());
                                if (cir30 == null) {
                                    // 创建cir30
                                    CollageInRecord30 newCir30 = new CollageInRecord30();
                                    newCir30.setModeId(o.getCollageModeId());
                                    newCir30.setUserId(cu.getId());
                                    newCir30.setStatus("0");
                                    newCir30.setTotalQuantity(1);
                                    newCir30.setUpdateTime(date);
                                    newCir30.setPayAmount(o.getPayAmount());
                                    tc.collageInRecord30Dao.save(newCir30);
                                    // 添加详情
                                    CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                    newCir30Copy.setIsTack("1"); // 提了
                                    newCir30Copy.setCir30Id(newCir30.getId());
                                    newCir30Copy.setOrderNo(o.getOrderNo());
                                    newCir30Copy.setPayAmount(o.getPayAmount());
                                    tc.collageInRecord30CopyDao.save(newCir30Copy);
                                    // 发货
                                    o.setDealStatus("1");
                                    tc.ordersDao.update(o);
                                } else {
                                    // 判断是否需要返钱
                                    if (cir30.getTotalQuantity() == 5) {
                                        // 开始退钱，退未提货的钱
                                        Account cuAcc = tc.accountDao.queryAccountByUserId(cu.getId());
                                        Double d = 0.0;
                                        List<CollageInRecord30Copy> cir30CopyList = tc.collageInRecord30CopyDao.queryByCir30Id(cir30.getId());
                                        if (cir30CopyList.size() > 0) {
                                            for (CollageInRecord30Copy cir30Copy : cir30CopyList) {
                                                cir30Copy.setIsTack("1"); // 修改为已提货
                                                d += cir30Copy.getPayAmount().doubleValue();
                                                tc.collageInRecord30CopyDao.update(cir30Copy);
                                                Orders orders = tc.ordersDao.queryByOrderNo(cir30Copy.getOrderNo());
                                                orders.setDealStatus("3"); // 已完成
                                                orders.setConfirmTime(new Date());
                                                orders.setRemarks("拼团专区内累计拼中六次! 当前订单自动完成");
                                                tc.ordersDao.update(orders);
                                            }
                                        }
                                        cuAcc.setVoucherBalance(cuAcc.getVoucherBalance().add(BigDecimal.valueOf(d))); //余额
                                        tc.accountDao.update(cuAcc);
                                        // 加个ar记录
                                        AccountRecord ar = new AccountRecord();
                                        ar.setIntoUserId(cu.getId());
                                        ar.setUpdateTime(date);
                                        ar.setType("0");
                                        ar.setAmount(BigDecimal.valueOf(d));
                                        ar.setNowAmount(cuAcc.getVoucherBalance());
                                        ar.setSign("1");
                                        ar.setOrderNo(o.getOrderNo());
                                        ar.setInfo("拼团专区内累计拼中六次! 广告补贴：" + d);
                                        tc.accountRecordDao.save(ar);
                                        // 改为退完
                                        cir30.setStatus("1"); // 已退
                                        tc.collageInRecord30Dao.update(cir30);
                                        // 把第六次做为新的第一次并发货
                                        // 发货
                                        o.setDealStatus("1");
                                        tc.ordersDao.update(o);
                                        // 创建cir30
                                        CollageInRecord30 newCir30 = new CollageInRecord30();
                                        newCir30.setModeId(o.getCollageModeId());
                                        newCir30.setUserId(cu.getId());
                                        newCir30.setStatus("0");
                                        newCir30.setTotalQuantity(1);
                                        newCir30.setUpdateTime(date);
                                        newCir30.setPayAmount(o.getPayAmount());
                                        tc.collageInRecord30Dao.save(newCir30);
                                        // 添加详情
                                        CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                        newCir30Copy.setIsTack("1"); // 提了
                                        newCir30Copy.setCir30Id(newCir30.getId());
                                        newCir30Copy.setOrderNo(o.getOrderNo());
                                        newCir30Copy.setPayAmount(o.getPayAmount());
                                        tc.collageInRecord30CopyDao.save(newCir30Copy);
                                    } else {
                                        // 继续累计
                                        cir30.setTotalQuantity(cir30.getTotalQuantity() + 1);
                                        cir30.setPayAmount(cir30.getPayAmount().add(o.getPayAmount()));
                                        tc.collageInRecord30Dao.update(cir30);
                                        // 添加详情
                                        CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                        newCir30Copy.setIsTack("0"); // 没有提
                                        newCir30Copy.setCir30Id(cir30.getId());
                                        newCir30Copy.setOrderNo(o.getOrderNo());
                                        newCir30Copy.setPayAmount(o.getPayAmount());
                                        tc.collageInRecord30CopyDao.save(newCir30Copy);
                                        // 代售
                                        o.setDealStatus("7");
                                        tc.ordersDao.update(o);
                                    }
                                }
                            }
                        }
                    }
//                Integer cha = quantity - rowSize;
////                // 准备加入虚拟数据，让团成立
////                List<Customer> cList = tc.customerDao.queryByFiction();
////                for (int i = 0; i < cha; i++) {
////                    int userId = 100 + (int) (Math.random() * (cList.size() + 1 - 1));
////                    Customer customer = tc.customerDao.queryById(userId);
////                    if (customer == null) {
////                        customer = tc.customerDao.queryById(666);
////                    }
////                    CollageUser newCU = new CollageUser();
////                    newCU.setModeProductId(mp.getId());
////                    newCU.setUserId(customer.getId());
////                    newCU.setUpdateTime(new Date());
////                    newCU.setFiction("1"); // 虚拟数据
////                    tc.collageUserDao.save(newCU);
////                }
                }
            }
        }
    }


    // 更新团队总人数,执行更新
    //@Scheduled(cron = "0 5 9,13,18,23 * * ?")
    public void TimingBusiness19() {
        System.err.println("========== >>>>>>>>>> 更新团队总人数开始");
        List<Customer> customerList = tc.customerDao.queryByIsNotFictionOr1198();
        if(customerList.size() > 0){
            for(Customer cu : customerList){
                int i = tc.customerService.queryTeamTotalNumber(cu.getId());
                cu.setSign("" + i);
                tc.customerDao.update(cu);
            }
        }
        System.err.println("========== >>>>>>>>>> 更新团队总人数结束");
    }


    // 更新顶点这个人分所有金额的5%
    //@Scheduled(cron = "0 10 0 * * ?")
    public void TimingBusiness20(){
        Customer customer = tc.customerDao.queryCustomerByAgentCode("BEE818202");
        if(customer != null){
            // 总金额
            BigDecimal totalAmount =  tc.todayCollageVoucherDao.queryByTotal();
            String info = "获得昨日所有金额的5%";
            BigDecimal ar = tc.accountRecordDao.queryByInfo(info);
            BigDecimal cha = totalAmount.multiply(BigDecimal.valueOf(0.05)).subtract(ar);
            Account account = tc.accountDao.queryAccountByUserId(customer.getId());
            account.setCollageBalance(account.getCollageBalance().add(cha));
            tc.accountDao.update(account);
            AccountRecord ar1 = new AccountRecord();
            ar1.setIntoUserId(customer.getId());
            ar1.setUpdateTime(new Date());
            ar1.setType("8");
            ar1.setAmount(cha);
            ar1.setNowAmount(account.getCollageBalance());
            ar1.setSign("1");
            ar1.setInfo("获得昨日所有金额的5%：" + cha);
            tc.accountRecordDao.save(ar1);
        }
    }


    // 更新团队有效人数,执行更新 -- 有需要时打开
    //@Scheduled(cron = "0 0 6 * * ?")
    public void TimingBusiness21() {
        System.err.println("========== >>>>>>>>>> 更新团队有效人数开始");
        List<Customer> customerList = tc.customerDao.queryByIsNotFictionOr1198();
        if(customerList.size() > 0){
            for(Customer cu : customerList){
                int rowSize = 0;
                List<AppMailListDTO> list1 = tc.customerDao.queryByMailList_1(cu.getId());
                if(list1.size() > 0){
                    for(AppMailListDTO dto1 : list1){

                        List<Orders> ordersList1 = tc.ordersDao.queryOrdersBy30Or40(dto1.getUserId());
                        if(ordersList1.size() > 0){
                            rowSize += 1;
                        }
                        List<AppMailListDTO> list2 = tc.customerDao.queryByMailList_1(dto1.getUserId());
                        if(list2.size() > 0){
                            for(AppMailListDTO dto2 : list2){

                                List<Orders> ordersList2 = tc.ordersDao.queryOrdersBy30Or40(dto2.getUserId());
                                if(ordersList2.size() > 0){
                                    rowSize += 1;
                                }
                                List<AppMailListDTO> list3 = tc.customerDao.queryByMailList_1(dto2.getUserId());
                                if(list3.size() > 0){
                                    for(AppMailListDTO dto3 : list3){

                                        List<Orders> ordersList3 = tc.ordersDao.queryOrdersBy30Or40(dto3.getUserId());
                                        if(ordersList3.size() > 0){
                                            rowSize += 1;
                                        }
                                        List<AppMailListDTO> list4 = tc.customerDao.queryByMailList_1(dto3.getUserId());
                                        if(list4.size() > 0){
                                            for(AppMailListDTO dto4 : list4){

                                                List<Orders> ordersList4 = tc.ordersDao.queryOrdersBy30Or40(dto4.getUserId());
                                                if(ordersList4.size() > 0){
                                                    rowSize += 1;
                                                }
                                                List<AppMailListDTO> list5 = tc.customerDao.queryByMailList_1(dto4.getUserId());
                                                if(list5.size() > 0){
                                                    for(AppMailListDTO dto5 : list5) {

                                                        List<Orders> ordersList5 = tc.ordersDao.queryOrdersBy30Or40(dto5.getUserId());
                                                        if(ordersList5.size() > 0){
                                                            rowSize += 1;
                                                        }
                                                        List<AppMailListDTO> list6 = tc.customerDao.queryByMailList_1(dto5.getUserId());
                                                        if(list6.size() > 0){

                                                            for(AppMailListDTO dto6 : list6){
                                                                List<Orders> ordersList6 = tc.ordersDao.queryOrdersBy30Or40(dto6.getUserId());
                                                                if(ordersList6.size() > 0){
                                                                    rowSize += 1;
                                                                }
                                                                List<AppMailListDTO> list7 = tc.customerDao.queryByMailList_1(dto6.getUserId());
                                                                if(list7.size() > 0){
                                                                    for(AppMailListDTO dto7 : list7){
                                                                        List<Orders> ordersList7 = tc.ordersDao.queryOrdersBy30Or40(dto7.getUserId());
                                                                        if(ordersList7.size() > 0){
                                                                            rowSize += 1;
                                                                        }
                                                                        List<AppMailListDTO> list8 = tc.customerDao.queryByMailList_1(dto7.getUserId());
                                                                        if(list8.size() > 0){
                                                                            for(AppMailListDTO dto8 : list8){
                                                                                List<Orders> ordersList8 = tc.ordersDao.queryOrdersBy30Or40(dto8.getUserId());
                                                                                if(ordersList8.size() > 0){
                                                                                    rowSize += 1;
                                                                                }
                                                                                List<AppMailListDTO> list9 = tc.customerDao.queryByMailList_1(dto8.getUserId());
                                                                                if(list9.size() > 0){
                                                                                    for(AppMailListDTO dto9 : list9){
                                                                                        List<Orders> ordersList9 = tc.ordersDao.queryOrdersBy30Or40(dto9.getUserId());
                                                                                        if(ordersList9.size() > 0){
                                                                                            rowSize += 1;
                                                                                        }
                                                                                        List<AppMailListDTO> list10 = tc.customerDao.queryByMailList_1(dto9.getUserId());
                                                                                        if(list10.size() > 0){
                                                                                            for(AppMailListDTO dto10 : list10){
                                                                                                List<Orders> ordersList10 = tc.ordersDao.queryOrdersBy30Or40(dto10.getUserId());
                                                                                                if(ordersList10.size() > 0){
                                                                                                    rowSize += 1;
                                                                                                }
                                                                                                List<AppMailListDTO> list11 = tc.customerDao.queryByMailList_1(dto10.getUserId());
                                                                                                if(list11.size() > 0){
                                                                                                    for(AppMailListDTO dto11 : list11){
                                                                                                        List<Orders> ordersList11 = tc.ordersDao.queryOrdersBy30Or40(dto11.getUserId());
                                                                                                        if(ordersList11.size() > 0){
                                                                                                            rowSize += 1;
                                                                                                        }
                                                                                                        List<AppMailListDTO> list12 = tc.customerDao.queryByMailList_1(dto11.getUserId());
                                                                                                        if(list12.size() > 0){
                                                                                                            for(AppMailListDTO dto12 : list12){
                                                                                                                List<Orders> ordersList12 = tc.ordersDao.queryOrdersBy30Or40(dto12.getUserId());
                                                                                                                if(ordersList12.size() > 0){
                                                                                                                    rowSize += 1;
                                                                                                                }
                                                                                                                List<AppMailListDTO> list13 = tc.customerDao.queryByMailList_1(dto12.getUserId());
                                                                                                                if(list13.size() > 0){
                                                                                                                    for(AppMailListDTO dto13 : list13){
                                                                                                                        List<Orders> ordersList13 = tc.ordersDao.queryOrdersBy30Or40(dto13.getUserId());
                                                                                                                        if(ordersList13.size() > 0){
                                                                                                                            rowSize += 1;
                                                                                                                        }
                                                                                                                        List<AppMailListDTO> list14 = tc.customerDao.queryByMailList_1(dto13.getUserId());
                                                                                                                        if(list14.size() > 0){
                                                                                                                            for(AppMailListDTO dto14 : list14){
                                                                                                                                List<Orders> ordersList14 = tc.ordersDao.queryOrdersBy30Or40(dto14.getUserId());
                                                                                                                                if(ordersList14.size() > 0){
                                                                                                                                    rowSize += 1;
                                                                                                                                }
                                                                                                                                List<AppMailListDTO> list15 = tc.customerDao.queryByMailList_1(dto14.getUserId());
                                                                                                                                if(list15.size() > 0){
                                                                                                                                    for(AppMailListDTO dto15 : list15){
                                                                                                                                        List<Orders> ordersList15 = tc.ordersDao.queryOrdersBy30Or40(dto15.getUserId());
                                                                                                                                        if(ordersList15.size() > 0){
                                                                                                                                            rowSize += 1;
                                                                                                                                        }
                                                                                                                                        List<AppMailListDTO> list16 = tc.customerDao.queryByMailList_1(dto15.getUserId());
                                                                                                                                        if(list16.size() > 0){
                                                                                                                                            for(AppMailListDTO dto16 : list16){
                                                                                                                                                List<Orders> ordersList16 = tc.ordersDao.queryOrdersBy30Or40(dto16.getUserId());
                                                                                                                                                if(ordersList16.size() > 0){
                                                                                                                                                    rowSize += 1;
                                                                                                                                                }
                                                                                                                                                List<AppMailListDTO> list17 = tc.customerDao.queryByMailList_1(dto16.getUserId());
                                                                                                                                                if(list17.size() > 0){
                                                                                                                                                    for(AppMailListDTO dto17 : list17){
                                                                                                                                                        List<Orders> ordersList17 = tc.ordersDao.queryOrdersBy30Or40(dto17.getUserId());
                                                                                                                                                        if(ordersList17.size() > 0){
                                                                                                                                                            rowSize += 1;
                                                                                                                                                        }
                                                                                                                                                        List<AppMailListDTO> list18 = tc.customerDao.queryByMailList_1(dto17.getUserId());
                                                                                                                                                        if(list18.size() > 0){
                                                                                                                                                            for(AppMailListDTO dto18 : list18){
                                                                                                                                                                List<Orders> ordersList18 = tc.ordersDao.queryOrdersBy30Or40(dto18.getUserId());
                                                                                                                                                                if(ordersList18.size() > 0){
                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                }
                                                                                                                                                                List<AppMailListDTO> list19 = tc.customerDao.queryByMailList_1(dto18.getUserId());
                                                                                                                                                                if(list19.size() > 0){
                                                                                                                                                                    for(AppMailListDTO dto19 : list19){
                                                                                                                                                                        List<Orders> ordersList19 = tc.ordersDao.queryOrdersBy30Or40(dto19.getUserId());
                                                                                                                                                                        if(ordersList19.size() > 0){
                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                        }
                                                                                                                                                                        List<AppMailListDTO> list20 = tc.customerDao.queryByMailList_1(dto19.getUserId());
                                                                                                                                                                        if(list20.size() > 0){
                                                                                                                                                                            for(AppMailListDTO dto20 : list20){
                                                                                                                                                                                List<Orders> ordersList20 = tc.ordersDao.queryOrdersBy30Or40(dto20.getUserId());
                                                                                                                                                                                if(ordersList20.size() > 0){
                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                }
                                                                                                                                                                                List<AppMailListDTO> list21 = tc.customerDao.queryByMailList_1(dto20.getUserId());
                                                                                                                                                                                if(list21.size() > 0){
                                                                                                                                                                                    for(AppMailListDTO dto21 : list21){
                                                                                                                                                                                        List<Orders> ordersList21 = tc.ordersDao.queryOrdersBy30Or40(dto21.getUserId());
                                                                                                                                                                                        if(ordersList21.size() > 0){
                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                        }
                                                                                                                                                                                        List<AppMailListDTO> list22 = tc.customerDao.queryByMailList_1(dto21.getUserId());
                                                                                                                                                                                        if(list22.size() > 0){
                                                                                                                                                                                            for(AppMailListDTO dto22 : list22){
                                                                                                                                                                                                List<Orders> ordersList22 = tc.ordersDao.queryOrdersBy30Or40(dto22.getUserId());
                                                                                                                                                                                                if(ordersList22.size() > 0){
                                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                                }
                                                                                                                                                                                                List<AppMailListDTO> list23 = tc.customerDao.queryByMailList_1(dto22.getUserId());
                                                                                                                                                                                                if(list23.size() > 0){
                                                                                                                                                                                                    for(AppMailListDTO dto23 : list23){
                                                                                                                                                                                                        List<Orders> ordersList23 = tc.ordersDao.queryOrdersBy30Or40(dto23.getUserId());
                                                                                                                                                                                                        if(ordersList23.size() > 0){
                                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                                        }
                                                                                                                                                                                                        List<AppMailListDTO> list24 = tc.customerDao.queryByMailList_1(dto23.getUserId());
                                                                                                                                                                                                        if(list24.size() > 0){
                                                                                                                                                                                                            for(AppMailListDTO dto24 : list24){
                                                                                                                                                                                                                List<Orders> ordersList24 = tc.ordersDao.queryOrdersBy30Or40(dto24.getUserId());
                                                                                                                                                                                                                if(ordersList24.size() > 0){
                                                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                                                }
                                                                                                                                                                                                                List<AppMailListDTO> list25 = tc.customerDao.queryByMailList_1(dto24.getUserId());
                                                                                                                                                                                                                if(list25.size() > 0){
                                                                                                                                                                                                                    for(AppMailListDTO dto25 : list25){
                                                                                                                                                                                                                        List<Orders> ordersList25 = tc.ordersDao.queryOrdersBy30Or40(dto25.getUserId());
                                                                                                                                                                                                                        if(ordersList25.size() > 0){
                                                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                        List<AppMailListDTO> list26 = tc.customerDao.queryByMailList_1(dto25.getUserId());
                                                                                                                                                                                                                        if(list26.size() > 0){
                                                                                                                                                                                                                            for(AppMailListDTO dto26 : list26){
                                                                                                                                                                                                                                List<Orders> ordersList26 = tc.ordersDao.queryOrdersBy30Or40(dto26.getUserId());
                                                                                                                                                                                                                                if(ordersList26.size() > 0){
                                                                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                List<AppMailListDTO> list27 = tc.customerDao.queryByMailList_1(dto26.getUserId());
                                                                                                                                                                                                                                if(list27.size() > 0){
                                                                                                                                                                                                                                    for(AppMailListDTO dto27 : list27){
                                                                                                                                                                                                                                        List<Orders> ordersList27 = tc.ordersDao.queryOrdersBy30Or40(dto27.getUserId());
                                                                                                                                                                                                                                        if(ordersList27.size() > 0){
                                                                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                        List<AppMailListDTO> list28 = tc.customerDao.queryByMailList_1(dto27.getUserId());
                                                                                                                                                                                                                                        if(list28.size() > 0){
                                                                                                                                                                                                                                            for(AppMailListDTO dto28 : list28){
                                                                                                                                                                                                                                                List<Orders> ordersList28 = tc.ordersDao.queryOrdersBy30Or40(dto28.getUserId());
                                                                                                                                                                                                                                                if(ordersList28.size() > 0){
                                                                                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                List<AppMailListDTO> list29 = tc.customerDao.queryByMailList_1(dto28.getUserId());
                                                                                                                                                                                                                                                if(list29.size() > 0){
                                                                                                                                                                                                                                                    for(AppMailListDTO dto29 : list29){
                                                                                                                                                                                                                                                        List<Orders> ordersList29 = tc.ordersDao.queryOrdersBy30Or40(dto29.getUserId());
                                                                                                                                                                                                                                                        if(ordersList29.size() > 0){
                                                                                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                        List<AppMailListDTO> list30 = tc.customerDao.queryByMailList_1(dto29.getUserId());
                                                                                                                                                                                                                                                        if(list30.size() > 0){
                                                                                                                                                                                                                                                            for(AppMailListDTO dto30 : list30){
                                                                                                                                                                                                                                                                List<Orders> ordersList30 = tc.ordersDao.queryOrdersBy30Or40(dto30.getUserId());
                                                                                                                                                                                                                                                                if(ordersList30.size() > 0){
                                                                                                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                List<AppMailListDTO> list31 = tc.customerDao.queryByMailList_1(dto30.getUserId());
                                                                                                                                                                                                                                                                if(list31.size() > 0){
                                                                                                                                                                                                                                                                    for(AppMailListDTO dto31 : list31){
                                                                                                                                                                                                                                                                        List<Orders> ordersList31 = tc.ordersDao.queryOrdersBy30Or40(dto31.getUserId());
                                                                                                                                                                                                                                                                        if(ordersList31.size() > 0){
                                                                                                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                        List<AppMailListDTO> list32 = tc.customerDao.queryByMailList_1(dto31.getUserId());
                                                                                                                                                                                                                                                                        if(list32.size() > 0){
                                                                                                                                                                                                                                                                            for(AppMailListDTO dto32 : list32){
                                                                                                                                                                                                                                                                                List<Orders> ordersList32 = tc.ordersDao.queryOrdersBy30Or40(dto32.getUserId());
                                                                                                                                                                                                                                                                                if(ordersList32.size() > 0){
                                                                                                                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                List<AppMailListDTO> list33 = tc.customerDao.queryByMailList_1(dto32.getUserId());
                                                                                                                                                                                                                                                                                if(list33.size() > 0){
                                                                                                                                                                                                                                                                                    for(AppMailListDTO dto33 : list33){
                                                                                                                                                                                                                                                                                        List<Orders> ordersList33 = tc.ordersDao.queryOrdersBy30Or40(dto33.getUserId());
                                                                                                                                                                                                                                                                                        if(ordersList33.size() > 0){
                                                                                                                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                        List<AppMailListDTO> list34 = tc.customerDao.queryByMailList_1(dto33.getUserId());
                                                                                                                                                                                                                                                                                        if(list34.size() > 0){
                                                                                                                                                                                                                                                                                            for(AppMailListDTO dto34 : list34){
                                                                                                                                                                                                                                                                                                List<Orders> ordersList34 = tc.ordersDao.queryOrdersBy30Or40(dto34.getUserId());
                                                                                                                                                                                                                                                                                                if(ordersList34.size() > 0){
                                                                                                                                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                List<AppMailListDTO> list35 = tc.customerDao.queryByMailList_1(dto34.getUserId());
                                                                                                                                                                                                                                                                                                if(list35.size() > 0){
                                                                                                                                                                                                                                                                                                    for(AppMailListDTO dto35 : list35){
                                                                                                                                                                                                                                                                                                        List<Orders> ordersList35 = tc.ordersDao.queryOrdersBy30Or40(dto35.getUserId());
                                                                                                                                                                                                                                                                                                        if(ordersList35.size() > 0){
                                                                                                                                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                        List<AppMailListDTO> list36 = tc.customerDao.queryByMailList_1(dto35.getUserId());
                                                                                                                                                                                                                                                                                                        if(list36.size() > 0){
                                                                                                                                                                                                                                                                                                            for(AppMailListDTO dto36 : list36){
                                                                                                                                                                                                                                                                                                                List<Orders> ordersList36 = tc.ordersDao.queryOrdersBy30Or40(dto36.getUserId());
                                                                                                                                                                                                                                                                                                                if(ordersList36.size() > 0){
                                                                                                                                                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                List<AppMailListDTO> list37 = tc.customerDao.queryByMailList_1(dto36.getUserId());
                                                                                                                                                                                                                                                                                                                if(list37.size() > 0){
                                                                                                                                                                                                                                                                                                                    for(AppMailListDTO dto37 : list37){
                                                                                                                                                                                                                                                                                                                        List<Orders> ordersList37 = tc.ordersDao.queryOrdersBy30Or40(dto37.getUserId());
                                                                                                                                                                                                                                                                                                                        if(ordersList37.size() > 0){
                                                                                                                                                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                        List<AppMailListDTO> list38 = tc.customerDao.queryByMailList_1(dto37.getUserId());
                                                                                                                                                                                                                                                                                                                        if(list38.size() > 0){
                                                                                                                                                                                                                                                                                                                            for(AppMailListDTO dto38 : list38){
                                                                                                                                                                                                                                                                                                                                List<Orders> ordersList38 = tc.ordersDao.queryOrdersBy30Or40(dto38.getUserId());
                                                                                                                                                                                                                                                                                                                                if(ordersList38.size() > 0){
                                                                                                                                                                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                List<AppMailListDTO> list39 = tc.customerDao.queryByMailList_1(dto38.getUserId());
                                                                                                                                                                                                                                                                                                                                if(list39.size() > 0){
                                                                                                                                                                                                                                                                                                                                    for(AppMailListDTO dto39 : list39){
                                                                                                                                                                                                                                                                                                                                        List<Orders> ordersList39 = tc.ordersDao.queryOrdersBy30Or40(dto39.getUserId());
                                                                                                                                                                                                                                                                                                                                        if(ordersList39.size() > 0){
                                                                                                                                                                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                        List<AppMailListDTO> list40 = tc.customerDao.queryByMailList_1(dto39.getUserId());
                                                                                                                                                                                                                                                                                                                                        if(list40.size() > 0){
                                                                                                                                                                                                                                                                                                                                            for(AppMailListDTO dto40 : list40){
                                                                                                                                                                                                                                                                                                                                                List<Orders> ordersList40 = tc.ordersDao.queryOrdersBy30Or40(dto40.getUserId());
                                                                                                                                                                                                                                                                                                                                                if(ordersList40.size() > 0){
                                                                                                                                                                                                                                                                                                                                                    rowSize += 1;
                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                List<AppMailListDTO> list41 = tc.customerDao.queryByMailList_1(dto40.getUserId());
                                                                                                                                                                                                                                                                                                                                                if(list41.size() > 0){
                                                                                                                                                                                                                                                                                                                                                    for(AppMailListDTO dto41 : list41){
                                                                                                                                                                                                                                                                                                                                                        List<Orders> ordersList41 = tc.ordersDao.queryOrdersBy30Or40(dto41.getUserId());
                                                                                                                                                                                                                                                                                                                                                        if(ordersList41.size() > 0){
                                                                                                                                                                                                                                                                                                                                                            rowSize += 1;
                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                            }
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                }
                                                                                                                                                                                                            }
                                                                                                                                                                                                        }
                                                                                                                                                                                                    }
                                                                                                                                                                                                }
                                                                                                                                                                                            }
                                                                                                                                                                                        }
                                                                                                                                                                                    }
                                                                                                                                                                                }
                                                                                                                                                                            }
                                                                                                                                                                        }
                                                                                                                                                                    }
                                                                                                                                                                }
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                cu.setEffective(rowSize);
                tc.customerDao.update(cu);
            }
        }
        System.err.println("========== >>>>>>>>>> 更新团队有效人数结束");
    }


    // 每天凌晨结算昨日的擂台奖金池，并清空昨日擂台累计（延迟10秒）-- 暂时不打开
    //@Scheduled(cron = "10 0 0 * * ?")
    public void TimingBusiness22(){
        // 系统当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date toDate = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(toDate);
        String date = sdf.format(now.getTime());
        now.add(Calendar.DAY_OF_MONTH, -1);
        String date1 = sdf.format(now.getTime());
        // 擂台奖金池 4%
        PrizePoolArean arean = tc.prizePoolAreanDao.queryByToday(date1); // 昨天的奖池
        if(arean == null){
            // 创建今日
            PrizePoolArean newArean = new PrizePoolArean();
            newArean.setUpdateTime(new Date());
            newArean.setSplitMoney(new BigDecimal(0));
            newArean.setPayMoney(new BigDecimal(0));
            tc.prizePoolAreanDao.save(newArean);
        }else{
            if(arean.getPayMoney().doubleValue() > 0){
                List<Customer> cuList = tc.customerDao.queryByTodayArean_1();
                if(cuList.size() > 0){
                    BigDecimal total = arean.getPayMoney(); // 总金额
                    BigDecimal one = total.multiply(BigDecimal.valueOf(0.3));
                    BigDecimal two = total.multiply(BigDecimal.valueOf(0.2));
                    BigDecimal three = total.multiply(BigDecimal.valueOf(0.1));
                    BigDecimal other = total.multiply(BigDecimal.valueOf(0.4)).divide(BigDecimal.valueOf(7), 4, BigDecimal.ROUND_HALF_UP);
                    for(int i = 0; i < cuList.size(); i++){
                        Customer cu = cuList.get(i);
                        Account account = tc.accountDao.queryAccountByUserId(cu.getId());
                        if(i == 0){
                            // 第1名 30%
                            account.setShareBalance(account.getShareBalance().add(one.multiply(BigDecimal.valueOf(0.7))));
                            account.setFrozenIn(account.getFrozenIn().add(one.multiply(BigDecimal.valueOf(0.3))));
                        }else if(i == 1){
                            // 第2名 20%
                            account.setShareBalance(account.getShareBalance().add(two.multiply(BigDecimal.valueOf(0.7))));
                            account.setFrozenIn(account.getFrozenIn().add(two.multiply(BigDecimal.valueOf(0.3))));
                        }else if(i == 2){
                            // 第3名 10%
                            account.setShareBalance(account.getShareBalance().add(three.multiply(BigDecimal.valueOf(0.7))));
                            account.setFrozenIn(account.getFrozenIn().add(three.multiply(BigDecimal.valueOf(0.3))));
                        }else{
                            // 第4-10名 平分40% / 7
                            account.setShareBalance(account.getShareBalance().add(other.multiply(BigDecimal.valueOf(0.7))));
                            account.setFrozenIn(account.getFrozenIn().add(other.multiply(BigDecimal.valueOf(0.3))));
                        }
                        tc.accountDao.update(account);
                        AccountRecord ar1 = new AccountRecord();
                        ar1.setIntoUserId(cu.getId());
                        ar1.setUpdateTime(new Date());
                        ar1.setType("10");
                        if(i == 0){
                            ar1.setAmount(one.multiply(BigDecimal.valueOf(0.7)));
                            ar1.setInfo("擂台排名第1名! 获得共享余额：" + one.multiply(BigDecimal.valueOf(0.7)));
                        }else if(i == 1){
                            ar1.setAmount(two.multiply(BigDecimal.valueOf(0.7)));
                            ar1.setInfo("擂台排名第2名! 获得共享余额：" + two.multiply(BigDecimal.valueOf(0.7)));
                        }else if(i == 1){
                            ar1.setAmount(three.multiply(BigDecimal.valueOf(0.7)));
                            ar1.setInfo("擂台排名第3名! 获得共享余额：" + three.multiply(BigDecimal.valueOf(0.7)));
                        }else{
                            ar1.setAmount(other.multiply(BigDecimal.valueOf(0.7)));
                            ar1.setInfo("擂台排名第4-6名! 获得共享余额：" + other.multiply(BigDecimal.valueOf(0.7)));
                        }
                        ar1.setNowAmount(account.getShareBalance());
                        ar1.setSign("1");
                        tc.accountRecordDao.save(ar1);
                        AccountRecord ar2 = new AccountRecord();
                        ar2.setIntoUserId(cu.getId());
                        ar2.setUpdateTime(new Date());
                        ar2.setType("10");
                        if(i == 0){
                            ar1.setAmount(one.multiply(BigDecimal.valueOf(0.3)));
                            ar1.setInfo("擂台排名第1名! 获得共享余额：(冻结)" + one.multiply(BigDecimal.valueOf(0.3)));
                        }else if(i == 1){
                            ar1.setAmount(two.multiply(BigDecimal.valueOf(0.3)));
                            ar1.setInfo("擂台排名第2名! 获得共享余额：(冻结)" + two.multiply(BigDecimal.valueOf(0.3)));
                        }else if(i == 1){
                            ar1.setAmount(three.multiply(BigDecimal.valueOf(0.3)));
                            ar1.setInfo("擂台排名第3名! 获得共享余额：(冻结)" + three.multiply(BigDecimal.valueOf(0.3)));
                        }else{
                            ar1.setAmount(other.multiply(BigDecimal.valueOf(0.3)));
                            ar1.setInfo("擂台排名第4-6名! 获得共享余额：(冻结)" + other.multiply(BigDecimal.valueOf(0.3)));
                        }
                        ar2.setNowAmount(account.getFrozenIn());
                        ar2.setSign("1");
                        tc.accountRecordDao.save(ar2);
                    }
                }
            }
        }
        // 清空所有人的今日擂台业绩
        List<Customer> allList = tc.customerDao.queryByTodayAreanAll();
        if(allList.size() > 0){
            for(Customer c : allList){
                c.setTodayArean(new BigDecimal(0));
                tc.customerDao.update(c);
            }
        }
    }


    // 分周红包 / 一周一次
    //@Scheduled(cron = "0 35 9 * * ?")
    public void TimingBusiness23(){
        // SELECT * FROM customer WHERE level_copy3_id = 4 AND is_active_copy3 = 1  #王者
        // SELECT * FROM customer WHERE level_copy3_id = 3 AND is_active_copy3 = 1 #高手
        // 直接按60-180分配
        List<Customer> customerList3 = tc.customerDao.queryByLevelCopyId3_3();
        int cl3 = customerList3.size();
        for(int i = 0; i < customerList3.size(); i++){
            Customer cu3 = customerList3.get(i);
            OpenRedPack orp = new OpenRedPack();
            orp.setUpdateTime(new Date());
            orp.setUserId(cu3.getId());
            orp.setRedPackQuantity(1);
            orp.setPrizeMoney(new BigDecimal(0));
            orp.setStatus("2"); // 未开
            orp.setIsShow("1"); // 显示
            orp.setType("1");   // 周红包
            tc.openRedPackDao.save(orp);
        }
        List<Customer> customerList4 = tc.customerDao.queryByLevelCopyId3_4();
        int cl4 = customerList4.size();
        for(int u = 0; u < customerList4.size(); u++){
            Customer cu4 = customerList4.get(u);
            for(int j = 0; j < 10; j++){
                OpenRedPack orp = new OpenRedPack();
                orp.setUpdateTime(new Date());
                orp.setUserId(cu4.getId());
                orp.setRedPackQuantity(1);
                orp.setPrizeMoney(new BigDecimal(0));
                orp.setStatus("2"); // 未开
                orp.setIsShow("1"); // 显示
                orp.setType("1");   // 周红包
                tc.openRedPackDao.save(orp);
            }
        }
        PrizePoolWeekly ppw = tc.prizePoolWeeklyDao.queryById(95);
        if(ppw.getPayMoney().doubleValue() > 0){
            int x = cl3 + cl4 * 10;
            System.err.println("-------------------- 需要分配多少个：" + x);
            List<Double> al = RandomNumberUtil.splitRedPacket(ppw.getPayMoney().doubleValue(), x, 60, 180);
            System.err.println("-------------------- 分配的红包金额信息：" + al);
            if(al.size() > 0){
                System.err.println("-------------------- 分配后一共有多少个：" + al.size());
                System.err.println("-------------------- 开始保存周红包 --------------------");
                List<OpenRedPack> orpList = tc.openRedPackDao.queryByType_1();
                for(int s = 0; s < orpList.size(); s++){
                    OpenRedPack orp = orpList.get(s);
                    orp.setPrizeMoney(BigDecimal.valueOf(al.get(s)));
                    tc.openRedPackDao.update(orp);
                }
            }
        }
    }


}

